faqts : Computers : Programming : Languages : JavaScript : Language Core : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 19 people (47%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How to sort a multidimensional array based one the first element

Dec 24th, 2005 06:50
Stian Totland, Kumar S,


You will have to use a comparison function: 
myArray.sort(myNumberComparison);
myArray.reverse; // only if you want it reversed
function myNumberComparison(a,b){ // for numbers
	return a[0] - b[0];
}
function myStringComparison(a,b){ // for strings
	return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0;
}
Stian