Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  82] [ 7]  / answers: 1 / hits: 24213  / 14 Years ago, thu, august 19, 2010, 12:00:00

I have an array and I want to sort by the number field not the name.



var showIt = [
[nuCycleDate,19561100],
[ndCycleDate,19460700],
[neCycleDate,0],
[nlCycleDate,0]
];


Thanks


More From » javascript

 Answers
12

You can provide sort with a comparison function.


showIt.sort(function(a, b) {
return a[1] - b[1];
});

a and b are items from your array. sort expects a return value that is greater than zero, equal to zero, or less than zero. The first indicates a comes before b, zero means they are equal, and the last option means b first.


[#95870] Monday, August 16, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarettajb

Total Points: 678
Total Questions: 94
Total Answers: 90

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;