Javascript sort then by

i'll just give a piece of code to show this Great Answer.

var a = [];
for (x = 1; x <= 3; x++){
for (y = 1; y <= 3; y++){
for (z = 1; z <= 3; z++){
a.push({x:x, y:y, z:z})
}
}
}
a


a.sort(function (a, b) {
    return a.z - b.z || a.x - b.x || a.y - b.y ;
});

just copy paste into the console and see for yourself.

for strings you'll need extra help

var a = [];
for (x = 1; x <= 3; x++){
for (y = 1; y <= 3; y++){
for (z = 1; z <= 3; z++){
a.push({x:x, y:y, z:'af' + z.toString()})
}
}
}

var strSort = function(a,b){
if(a < b) return -1;
    if(a > b) return 1;
    return 0;
}

a.sort(function (a, b) {
return strSort(a.z,b.z) || a.x - b.x || a.y - b.y ;
});

Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem