I have an object where each string contains an array:
var users = {
name : ["joe" , "tim" , "bob" , "joe"],
age : [23 , 22 , 21 , 20 ],
other : ["AA" , "BB" , "CC" , "DD"]
}
How can I sort these elements, for example by age, so I get this as a result?
var users = {
name : ["joe" , "tim" , "bob" , "joe"],
age : [ 20 , 21 , 22 , 23 ],
other : ["DD" , "CC" , "BB" , "AA" ]
}
I nserted a duplicate name on purpose, since it should work in cases like these (should keep the relationship positions).
Note: In case someone else has this same issue, after reading the answers I now recommend changing the data structure to:
var users = [ // note this is now an array
name : {"joe" , "tim" , "bob" , "joe"},
age : {23 , 22 , 21 , 20 },
other : {"AA" , "BB" , "CC" , "DD"}
]
This way, more simple approaches like .sort can be used.