Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
39
rated 0 times [  40] [ 1]  / answers: 1 / hits: 39231  / 14 Years ago, thu, may 13, 2010, 12:00:00

How would you sort a multidimensional array in JavaScript?



I have an array full of arrays that contain two dates and a string. I need the main array sorted by one of the date arrays, is this possible?



data stucture:



events = [
{ date 1, date 2, string },
{ date 2, date 2, string },
]

More From » arrays

 Answers
538

Duplicate of sort outer array based on values in inner array, javascript
here you will find several answers, like my own



var arr = [.....]
arr.sort((function(index){
return function(a, b){
return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
};
})(2)); // 2 is the index


This sorts on index 2


[#96801] Monday, May 10, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
triston

Total Points: 545
Total Questions: 88
Total Answers: 94

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;