Creative, Informative and Entertaining Stuff for everyone

Monday February 6th 2012

Categories

Archives

Calender

February 2012
S M T W T F S
« Dec    
 1234
567891011
12131415161718
19202122232425
26272829  

Sorting a two dimensional array in javascript

Bookmark and Share

function sortArray(x,y)
{
return ((x[1] < y[1]) ? -1 : ((x[1] > y[1]) ? 1 : 0));
}

var givenarr = [[0,8],[1,9],[2,7]];

givenarr.sort(sortArray);

========================

The result array will be givenarr = [[2,7],[0,8],[1,9]];

Leave a Reply