sorting

Sort strings by the first N characters

别说谁变了你拦得住时间么 提交于 2021-02-04 15:07:28
问题 I have a text file with lines like this: 2010-02-18 11:46:46.1287 bla 2010-02-18 11:46:46.1333 foo 2010-02-18 11:46:46.1333 bar 2010-02-18 11:46:46.1467 bla A simple sort would swap lines 2 and 3 (bar comes before foo), but I would like to keep lines (that have the same date/time) in their original order. How can I do this in Python? 回答1: sorted(array, key=lambda x:x[:24]) Example: >>> a = ["wxyz", "abce", "abcd", "bcde"] >>> sorted(a) ['abcd', 'abce', 'bcde', 'wxyz'] >>> sorted(a, key=lambda

Sort strings by the first N characters

ぐ巨炮叔叔 提交于 2021-02-04 15:07:27
问题 I have a text file with lines like this: 2010-02-18 11:46:46.1287 bla 2010-02-18 11:46:46.1333 foo 2010-02-18 11:46:46.1333 bar 2010-02-18 11:46:46.1467 bla A simple sort would swap lines 2 and 3 (bar comes before foo), but I would like to keep lines (that have the same date/time) in their original order. How can I do this in Python? 回答1: sorted(array, key=lambda x:x[:24]) Example: >>> a = ["wxyz", "abce", "abcd", "bcde"] >>> sorted(a) ['abcd', 'abce', 'bcde', 'wxyz'] >>> sorted(a, key=lambda

Why is OrderBy which returns IOrderedEnumerable<T> much faster than Sort?

a 夏天 提交于 2021-02-04 14:30:14
问题 This is a follow up of this excellent question C# Sort and OrderBy comparison. I will use the same example: List<Person> persons = new List<Person>(); persons.Add(new Person("P005", "Janson")); persons.Add(new Person("P002", "Aravind")); persons.Add(new Person("P007", "Kazhal")); The methods in contention are: persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true)); //and persons.OrderBy(n => n.Name); Let me start by saying that I understand there isn't any significant performance

Why is OrderBy which returns IOrderedEnumerable<T> much faster than Sort?

♀尐吖头ヾ 提交于 2021-02-04 14:29:06
问题 This is a follow up of this excellent question C# Sort and OrderBy comparison. I will use the same example: List<Person> persons = new List<Person>(); persons.Add(new Person("P005", "Janson")); persons.Add(new Person("P002", "Aravind")); persons.Add(new Person("P007", "Kazhal")); The methods in contention are: persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true)); //and persons.OrderBy(n => n.Name); Let me start by saying that I understand there isn't any significant performance

unix sort for 2 fields numeric order

心不动则不痛 提交于 2021-02-04 13:56:26
问题 I need to sort some data with unix sort but I can't figure exactly the right syntax, the data looks like 3.9.1 Step 10: 3.9.1 Step 20: 3.8.10 Step 20: 3.10.2 Step 10: 3.8.4 Step 90: 3.8.4 Step 100: 3.8.4 Step 10: I want to sort it using first the major number, then the step number, e.g. the data sorted above would look like. 3.8.4 Step 10: 3.8.4 Step 90: 3.8.4 Step 100: 3.8.10 Step 20: 3.9.1 Step 10: 3.9.1 Step 20: 3.10.2 Step 10: I have found the way to sort by first number on this site:

Sort a list that contains a custom class

风格不统一 提交于 2021-02-04 13:48:30
问题 so I'm currently doing an exercise for college that has several optional parts (because we havn't done this in class yet), one of them being to use lists instead of arrays (so it'd be variable size) and another one printing the list sorted by points (I'll get to that now) So, I have the Player.java class which looks like this. public class Player { String name; String password; int chips; int points; public Player(String n, String pw, int c, int p) { name = n; password = pw; chips = c; points

Sort a list that contains a custom class

为君一笑 提交于 2021-02-04 13:47:12
问题 so I'm currently doing an exercise for college that has several optional parts (because we havn't done this in class yet), one of them being to use lists instead of arrays (so it'd be variable size) and another one printing the list sorted by points (I'll get to that now) So, I have the Player.java class which looks like this. public class Player { String name; String password; int chips; int points; public Player(String n, String pw, int c, int p) { name = n; password = pw; chips = c; points

PHP How to sort an array of objects based on priority order with a backup of alphabetical?

旧巷老猫 提交于 2021-02-04 08:39:27
问题 I have an array like this: $sort_me = array( array("file"=>"Desert.jpg"), array("file"=>"Hello.jpg"), array("file"=>"Test.jpg) ) I want to sort this array based on the file attribute alphabetically. To complicate matters I have an optional array: $sort_order = array("Test.jpg", "Hello.jpg", "NotFound.jpg") This array will specify some user defined ordering. These array elements have priority, and will be placed in that order if they are found. Other elements not matching anything in $sort

Sorting an array with bubble sort [duplicate]

我的梦境 提交于 2021-02-04 08:23:39
问题 This question already has answers here : How can I sort arrays and data in PHP? (12 answers) Closed last year . I am trying to create an algorithm that shows each step of bubble sort, sorting one number at a time. I was was able to sort the number at the first index, but I need to figure out how to sort all the numbers. $x = array (9,7,5,3,0); $count = count($x); for($i = 0; $i < $count-1; $i++ ) { $temp = $x[$i+1]; $x[$i+1] = $x[$i]; $x[$i] = $temp; echo '<pre>'; print_r($x); } My current

Values sorted as String but I want to sort them as number in Python

老子叫甜甜 提交于 2021-02-04 08:19:07
问题 I read in python a log that contains name, memory, ncalls for each row and save this as tuple list where each element is a tuple (name, memory, ncalls) sometimes need to sort the list according name other times according memory or ncalls. The problem if I simply use the code mylist=sorted(mylist, key=itemgetter(2)) the list is sorted using the desired parameter but python consider the parameter like a String and I get this result item3, 45, 1 item1, 4, 12 item4, 65, 3 item2, 65, 5 the desired