This example is for sorting arrays by numbers (and dates) or strings.
Array.prototype.deepsort= function(){ var i, order= arguments, L= order.length, tem; return this.sort(function(a, b){ i= 0; while(i < L){ tem= order[i++]; var ao= a[tem] || 0, bo= b[tem] || 0; if(ao== bo) continue; return ao> bo? 1: -1; } return 0; }); } var a= [ [ 'z', 1, 0 ], [ 'a', 0, 1 ],['m',-1,10] ,['a','1',-1]]; alert(a.deepsort(0,1,2)+'\n\n'+a.deepsort(2,0,1))
Sort by the selected index (passed as an argument).
If the elements in this index in each array match, sorts by the next index passed as an argument, if any.
Continue until the elements match and there are more arguments.
You do not need to specify more than one index to sort by
a.deepsort(0); a.deepsort(2);
kennebec
source share