two factors JSON sorting
I found an exellent script example to sorts a JSON according to a specified key:
function sortJSON(data, key, way) {
return data.sort(function(a, b) {
var x = a[key]; var y = b[key];
if (way === '123' ) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }
if (way === '321') { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }
});
}
where:
data = JSON to sort
key = the key you want it sorted with
way = ascending or descending order
My JSON is as follow:
var POL = [
{cName:"John Doe", cDate: new Date(2005, 4, 16), cPassword:"162151", cTeam:34, cEmail:"john.doe@email.com"},
{cName:"Jane Doe", cDate: new Date(2005, 4, 16), cPassword:"161974", cTeam:34, cEmail:"jane.doe@email.com"},
{cName:"Jack Doe", cDate: new Date(2013, 3, 22), cPassword:"167406", cTeam:34, cEmail:"jack.doe@email.com"},
...
]
I need to sort the JSON by cDate first but supposed there are identical dates, I need to sort these by cPassword
How can I update my script, maybe I need something totally different or maybe is it even possible?
