sorting

Sort array of objects by multiple properties in Javascript

本小妞迷上赌 提交于 2020-01-11 13:00:50
问题 I've been reading similar posts all day but can't figure out how to sort my javascript array by multiple properties. My array has a 'name' and 'type' property. To sort by name I now use: byNameDesc.sort(function (a, b) { var x = a.name.toLowerCase(); var y = b.name.toLowerCase(); return y < x ? -1 : y > x ? 1 : 0; }); Works great. I want to enhance this function. If 'name' is 'foo' it should always be on top. And I also want to sort by 'type'. So 'foo' should always be on top, next sort by

Sort array of objects by multiple properties in Javascript

删除回忆录丶 提交于 2020-01-11 12:59:08
问题 I've been reading similar posts all day but can't figure out how to sort my javascript array by multiple properties. My array has a 'name' and 'type' property. To sort by name I now use: byNameDesc.sort(function (a, b) { var x = a.name.toLowerCase(); var y = b.name.toLowerCase(); return y < x ? -1 : y > x ? 1 : 0; }); Works great. I want to enhance this function. If 'name' is 'foo' it should always be on top. And I also want to sort by 'type'. So 'foo' should always be on top, next sort by

Java Multi-Level Comparator

喜夏-厌秋 提交于 2020-01-11 12:34:47
问题 I am working on a problem where I must take these "Song-artist pairs" from an input file and sort alphabetically. The guidelines to the sorting goes like so: Should sort the song-artist pairs by the name of the author first. Once sorted by the artists, if there are multiple songs by the same artist, they should be sort alphabetically as well. If the artists name starts with "The", ignore it for sorting purposes. My problem is that when I am sorting these, I am able to get the artists sorted

Ruby: Sorting 2 arrays using values from one of them

╄→尐↘猪︶ㄣ 提交于 2020-01-11 12:27:21
问题 I'm creating a simple game in ruby, and I have two arrays storing high score: $HS_Points and $HS_Names . I'm saving high score in two files, and I wanted to encode points a bit (the points are converted with .to_s(23) ). I wanted to sort names and scores in descending order and limit them to 10. So, I need to: Check whether amount of points player scored is higher than amounts of points in high score (on index 0 to 9). If the score received is enough to set player on top 10, put the points

Ruby: Sorting 2 arrays using values from one of them

社会主义新天地 提交于 2020-01-11 12:27:09
问题 I'm creating a simple game in ruby, and I have two arrays storing high score: $HS_Points and $HS_Names . I'm saving high score in two files, and I wanted to encode points a bit (the points are converted with .to_s(23) ). I wanted to sort names and scores in descending order and limit them to 10. So, I need to: Check whether amount of points player scored is higher than amounts of points in high score (on index 0 to 9). If the score received is enough to set player on top 10, put the points

sort array by value that is calculated from the array

喜欢而已 提交于 2020-01-11 12:04:07
问题 I have the lat and lng from a user from the database in an array and I have my lat and lng Now I want to calculate the distance and sort the users from my database with that $mylat = $_SESSION['lat']; $mylng = $_SESSION['lng']; $statement = $pdo->prepare("SELECT * FROM users"); $statement->execute(); $users = $statement->fetchAll(); foreach($users as $row){ $dist = 0.0; $x1 = $mylng; $x2 = $row['lng']; $y1 = $mylat; $y2 = $row['lat']; $dist = acos(sin($x1=deg2rad($x1))*sin($x2=deg2rad($x2))

VBA Excel Variable Sorting on Multiple Keys/Orders

旧巷老猫 提交于 2020-01-11 11:12:51
问题 How can I sort on multiple columns (keys/orders) at the same time? When I run my below code it re-sorts the data when executing each line instead of one sort with multiple columns in the sort. Changing the 'key' or 'order' number from 1 to 2, etc. does not help. Thanks. wsLast_Row = Cells(Rows.Count, 2).End(xlUp).Row Range("A3:BZ" & wsLast_Row).Sort key1:=Range("A3:A" & wsLast_Row), _ order1:=xlAscending, Header:=xlNo Range("A3:BZ" & wsLast_Row).Sort key1:=Range("B3:B" & wsLast_Row), _ order1

VBA Excel Variable Sorting on Multiple Keys/Orders

て烟熏妆下的殇ゞ 提交于 2020-01-11 11:12:41
问题 How can I sort on multiple columns (keys/orders) at the same time? When I run my below code it re-sorts the data when executing each line instead of one sort with multiple columns in the sort. Changing the 'key' or 'order' number from 1 to 2, etc. does not help. Thanks. wsLast_Row = Cells(Rows.Count, 2).End(xlUp).Row Range("A3:BZ" & wsLast_Row).Sort key1:=Range("A3:A" & wsLast_Row), _ order1:=xlAscending, Header:=xlNo Range("A3:BZ" & wsLast_Row).Sort key1:=Range("B3:B" & wsLast_Row), _ order1

Sort an array with special characters - iPhone

守給你的承諾、 提交于 2020-01-11 11:03:16
问题 I have an array with french strings let say: "égrener" and "exact" I would like to sort it such as égrener is the first. When I do: NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor]; NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; I get the é at the end of the list... What should I do? Thanks 回答1: There’s a convenience method in NSString that lets you do

Sort an array with special characters - iPhone

a 夏天 提交于 2020-01-11 11:00:48
问题 I have an array with french strings let say: "égrener" and "exact" I would like to sort it such as égrener is the first. When I do: NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor]; NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; I get the é at the end of the list... What should I do? Thanks 回答1: There’s a convenience method in NSString that lets you do