javascript - sort array multidimensional ! three level -
i want 3 level sort each asc or desc.
for example:
1st sort age(asc),then nationality(desc),then name(asc)
this using of works
function sortobjects(objarray, properties) { var primers = arguments[2] || {}; properties = properties.map(function(prop) { if( !(prop instanceof array) ) { prop = [prop, 'asc'] } if( prop[1] == 'desc' ) { prop[1] = -1; } else { prop[1] = 1; } return prop; }); function valuecmp(x, y) { return x > y ? 1 : x < y ? -1 : 0; } function arraycmp(a, b) { var arr1 = [], arr2 = []; properties.foreach(function(prop) { var avalue = a[prop[0]], bvalue = b[prop[0]]; if( typeof primers[prop[0]] != 'undefined' ) { avalue = primers[prop[0]](avalue); bvalue = primers[prop[0]](bvalue); } arr1.push( prop[1] * valuecmp(avalue, bvalue) ); arr2.push( prop[1] * valuecmp(bvalue, avalue) ); }); return arr1 < arr2 ? -1 : 1; } objarray.sort(function(a, b) { return arraycmp(a, b); }); }
i use this
sortobjects(array,[["age","asc"],["nationality","desc"],["name","asc"]]);
i want sort nationality[1] doesnt work...how sort first value of array?
sortobjects(array,[["age","asc"],["nationality[1]","desc"],["name","asc"]]);
how sort first value of array?
Comments
Post a Comment