sorting

Python - How to sort a list, by an attribute from another class

巧了我就是萌 提交于 2020-01-30 12:15:48
问题 I have some elements in a list in one class. I want them sorted in a new list, and they have to be sorted by an attribute from another class. Can anyone give an example? My code so far looks like this: class Carcompany: def __init__(self, model, production_number): self.model = model self.production_number = production_number self.car_list = [] def add_car_to_car_list(self, car): self.car_list.append(car) class Info: def __init__(self): self.license_plate_number = [] def add_license_plate_to

Python - How to sort a list, by an attribute from another class

守給你的承諾、 提交于 2020-01-30 12:15:08
问题 I have some elements in a list in one class. I want them sorted in a new list, and they have to be sorted by an attribute from another class. Can anyone give an example? My code so far looks like this: class Carcompany: def __init__(self, model, production_number): self.model = model self.production_number = production_number self.car_list = [] def add_car_to_car_list(self, car): self.car_list.append(car) class Info: def __init__(self): self.license_plate_number = [] def add_license_plate_to

Sort an array without rewriting a key

本秂侑毒 提交于 2020-01-30 09:01:37
问题 I have an array which looks like this: $arr = ( [0]=>Int(2) [1]=>Array( ....) [2]=>Array( ....) [3]=>Array( ....)) I have used usort to sort it: usort($arr,function($a, $b) { if($a['prop'] == $b['prop']) return 0; return ($a['prop'] < $b['prop']) ? 1 : -1; }); My problem is that the key [0] is rewritten with an Array element. Don't get me wrong...it's suppose to. How could i sort the $arr array without rewrite the key [0] ? 回答1: just use uasort() http://php.net/manual/en/function.uasort.php

ruby alphanumeric sort not working as expected

£可爱£侵袭症+ 提交于 2020-01-30 08:55:46
问题 Given the following array: y = %w[A1 A2 B5 B12 A6 A8 B10 B3 B4 B8] => ["A1", "A2", "B5", "B12", "A6", "A8", "B10", "B3", "B4", "B8"] With the expected sorted array to be: => ["A1", "A2", "A6", "A8", "B3", "B4", "B5", "B8", "B10", "B12"] Using the following (vanilla) sort, I get: irb(main):2557:0> y.sort{|a,b| puts "%s <=> %s = %s\n" % [a, b, a <=> b]; a <=> b} A1 <=> A8 = -1 A8 <=> B8 = -1 A2 <=> A8 = -1 B5 <=> A8 = 1 B4 <=> A8 = 1 B3 <=> A8 = 1 B10 <=> A8 = 1 B12 <=> A8 = 1 A6 <=> A8 = -1 A1

ruby alphanumeric sort not working as expected

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-30 08:55:33
问题 Given the following array: y = %w[A1 A2 B5 B12 A6 A8 B10 B3 B4 B8] => ["A1", "A2", "B5", "B12", "A6", "A8", "B10", "B3", "B4", "B8"] With the expected sorted array to be: => ["A1", "A2", "A6", "A8", "B3", "B4", "B5", "B8", "B10", "B12"] Using the following (vanilla) sort, I get: irb(main):2557:0> y.sort{|a,b| puts "%s <=> %s = %s\n" % [a, b, a <=> b]; a <=> b} A1 <=> A8 = -1 A8 <=> B8 = -1 A2 <=> A8 = -1 B5 <=> A8 = 1 B4 <=> A8 = 1 B3 <=> A8 = 1 B10 <=> A8 = 1 B12 <=> A8 = 1 A6 <=> A8 = -1 A1

Sort an ArrayList by an object's attribute in java [duplicate]

怎甘沉沦 提交于 2020-01-30 08:40:43
问题 This question already has answers here : Sorting an ArrayList of objects using a custom sorting order (10 answers) Closed 5 months ago . I wish to sort my objects in order of their Email address. This is the method I've attempted but it does not work, but I'm not even sure it's the correct way to do what I want? public static ArrayList<Billing> sortedListByEmail(ArrayList<Billing> Billing) { ArrayList<Billing> Sort = new ArrayList<Billing>(); for (int i = 0; i < Sort.size(); i++) {

Pythonic custom sort for letter grades 'D', 'C-' ,…, 'A+'?

不羁岁月 提交于 2020-01-30 08:29:04
问题 Is there a more Pythonic, compact, intuitive way to sort letter-grades than this (without using a custom dict)? grades = ['B-','C','B','C+','A','D+','B+','C-','A+','D','A-'] sorted(grades, key=lambda g: (g[0], '+ -'.index((g+' ')[1])) ) ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D'] In order to get the comparative numerical order of 'X-','X','X+', I do a hacky append of a space ' ' so that g[1] always exists, so then I can use .index() to get the rank of the modifier '+ -'.

XSLT unable to group/sort nodes based on value

浪尽此生 提交于 2020-01-30 08:24:05
问题 I'm trying to transform this xml. However I'm having formatting issues. Could someone please guide me to solve this problem. Thanks in advance <?xml version="1.0" encoding="windows-1252"?> <XML> <Attributes> <Attribute> <id>5</id> <Name>Buyer ID</Name> <Type>common</Type> <Value>Lee</Value> </Attribute> <Attribute> <id>331</id> <Name>Enviornment</Name> <Type>common</Type> <Value>Development</Value> </Attribute> <Attribute> <id>79</id> <Name>Retail</Name> <Type>common</Type> <Value></Value> <

Store name + number and sort in size order based on the numbers

主宰稳场 提交于 2020-01-30 08:18:25
问题 Doing a college project and I'm a bit stuck.. Basically, I need to take the string input for an employee name and an integer input for the amount of properties they sold. I then need to print these values in descending order based on the number of properties sold, so, for example, I need to print something like this: (Bolded to make it easier to read) Name : John Properties Sold : 5 Name : Peter Properties Sold : 4 Name : Craig Properties Sold : 3 I have stored the name and properties sold in

How do I sort a multidimensional table in Lua?

孤街醉人 提交于 2020-01-30 05:43:05
问题 I have a table consisting basically of the following: myTable = {{1, 6.345}, {2, 3.678}, {3, 4.890}} and I'd like to sort the table by the decimal values. So I'd like to the output to be: {{2, 3.678}, {3, 4.890}, {1, 6.345}} If possible, I'd like to use the table.sort() function. Thankyou in advance for the help :-) 回答1: Given that your table is a sequence, you can use table.sort directly. This function accepts a comparison predicate as its second argument, which prescribes the comparison