sooon
sooon

Reputation: 4878

Javascript: sort multidimension array

I have a multidimension array:

var somearray = new Array(
["110", "210", "310"] ,
["020", "120", "220"] ,
["020", "120", "200"] ,
["010", "120", "230"] ,
["130", "220", "310"] ,
["103", "113", "123"] ,
...
);

And I want to sort it with priority of first column, then second column then third column. How can I do that methodologically? Thanks!

Upvotes: 0

Views: 81

Answers (1)

Chris Charles
Chris Charles

Reputation: 4446

Simple:

somearray.sort(function(a,b){
  if (a[0]!=b[0]) return a[0]-b[0];
  if (a[1]!=b[1]) return a[1]-b[1];
  return a[2]-b[2];
}); 

Upvotes: 7

Related Questions