sorting

How to sort list of lists by highest number?

99封情书 提交于 2021-01-28 15:21:58
问题 This is my list: [['Alfred Jeffries', '9'], ['Peter Smalls', '10'], ['Bob Daniels', '8']] I want to sort this numerically, highest to lowest. Expected outcome: [['Peter Smalls', '10'], ['Alfred Jeffries', '9'], ['Bob Daniels', '8']] Thanks! 回答1: Use key to specify a function of one argument that is used to extract a comparison key from each list element. The argument reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed. >>> l = [[

How to sort list of lists by highest number?

。_饼干妹妹 提交于 2021-01-28 15:19:53
问题 This is my list: [['Alfred Jeffries', '9'], ['Peter Smalls', '10'], ['Bob Daniels', '8']] I want to sort this numerically, highest to lowest. Expected outcome: [['Peter Smalls', '10'], ['Alfred Jeffries', '9'], ['Bob Daniels', '8']] Thanks! 回答1: Use key to specify a function of one argument that is used to extract a comparison key from each list element. The argument reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed. >>> l = [[

how to sort list of songs from external storage alphabetically in flutter

筅森魡賤 提交于 2021-01-28 15:01:05
问题 As my Problem Says..i want to sort my listView items that are basically songs read by external storage..I want to sort them alphabetically..this is my code for listView final List<MaterialColor> _colors = Colors.primaries; @override Widget build(BuildContext context) { final rootIW = MPInheritedWidget.of(context); SongData songData = rootIW.songData; return new ListView.builder( itemCount: songData.songs.length, itemBuilder: (context, int index) { var s = songData.songs[index]; final

Sorting an array with .sort((a,b) => a>b) works. Why?

时间秒杀一切 提交于 2021-01-28 14:09:47
问题 [9,2,1,80].sort((a,b) => a>b) // gives [ 1, 2, 9, 80 ] [9,2,1,80].sort((a,b) => a<b) // gives [ 80, 9, 2, 1 ] Why? I have some code that uses the above comparison function. A comparison function for numbers should be something like (a,b) => a-b. Why the above code is correct, if it is? 回答1: It works sometimes - depending on your browser and the input array - because sort expects either a positive number, 0 or a negative number as return value. The expression a>b or a<b returns a boolean which

Python: Sorted file list

怎甘沉沦 提交于 2021-01-28 13:34:30
问题 I use os.path to generate a file list from a directory. I am generating a photo gallery from it via Tkinter. However the sorting is completely random. I do not see a greater logic behind the order of the photos, that are displayed from the directory. And when I print the list, it's random as well. How can I change the order of the list, coming out of this snippet by file name or date modified? image_list = [os.path.join("/home/pi/fotos/previews",fn) for fn in next(os.walk("/home/pi/fotos

Python: Sorted file list

喜夏-厌秋 提交于 2021-01-28 13:33:32
问题 I use os.path to generate a file list from a directory. I am generating a photo gallery from it via Tkinter. However the sorting is completely random. I do not see a greater logic behind the order of the photos, that are displayed from the directory. And when I print the list, it's random as well. How can I change the order of the list, coming out of this snippet by file name or date modified? image_list = [os.path.join("/home/pi/fotos/previews",fn) for fn in next(os.walk("/home/pi/fotos

Problem with std::sort function. Seem to always have null value for 1 element after 2 rounds of iteraion

别说谁变了你拦得住时间么 提交于 2021-01-28 12:25:50
问题 I am trying to write a sort program trying to sort a data set I have. The key to the sorting is Grid_ID and it happened to be an alpha-numeric data. I have tried to sort accordingly It give me an error terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0) On doing debugging, the reading part of the code seems to function. The reading of file content into DataContainer data get filled with the right data of

sort list of tuples with multiple criteria

社会主义新天地 提交于 2021-01-28 11:47:27
问题 I have a list of tuples of k elements. I'd like to sort with respect to element 0, then element 1 and so on and so forth. I googled but I still can't quite figure out how to do it. Would it be something like this? list.sort(key = lambda x : (x[0], x[1], ...., x[k-1]) In particular, I'd like to sort using different criteria, for example, descending on element 0, ascending on element 1 and so on. 回答1: Since python's sort is stable for versions after 2.2 (or perhaps 2.3), the easiest

How to split log file in bash based on time condition

六月ゝ 毕业季﹏ 提交于 2021-01-28 11:24:56
问题 I have simple log file with timestamp in milliseconds like this one: [02/03/2020 07:53:58.859000] 5 [02/03/2020 07:53:58.935300] 2 [02/03/2020 10:04:50.355600] 0 [02/03/2020 10:04:51.028900] 1 [02/03/2020 10:38:13.468200] 6 And I want to achieve better readability so +-2seconds separate logs by dashes like this one: [02/03/2020 07:53:58.859000] 5 [02/03/2020 07:53:58.935300] 2 ------------------------------ [02/03/2020 10:04:50.355600] 0 [02/03/2020 10:04:51.028900] 1 ------------------------

Sort documents by value in the last element of an array that matches filter. Mongodb

假装没事ソ 提交于 2021-01-28 11:24:17
问题 I have a collection of evaluationGroups with the following documents structure: { "_id" : ObjectId("60073749694fd4d81e4d677d"), "AlertSettingId" : ObjectId("5ffddaaa0b1d2c30b191599a"), "CurrentStatus" : "success", "Evaluations" : [ { "EvaluatedAt" : ISODate("2021-01-19T19:47:18.850Z"), "ReferenceValue" : 1.0, "Status" : "success" }, { "EvaluatedAt" : ISODate("2021-01-19T19:52:16.423Z"), "ReferenceValue" : 1.0, "Status" : "triggered" }, { "EvaluatedAt" : ISODate("2021-01-19T21:47:16.400Z"),