This question already has an answer here:
- Sorting an array of JavaScript objects 9 answers
Given the following:
var array = [{"name":"zoe", "hotdogs":5},{"name":"april", "hotdogs":5},{"name":"ryan", "hot dogs":8}]
How can I sort the elements of the array by name using JavaScript?
var array = [{"name":"april", "hotdogs":5},{"name":"ryan", "hotdogs":8},{"name":"zoe", "hotdogs":5}]
Is there a way to apply some function to sort the objects? Are there helper libraries that help with performance?
I tried the following:
array.sort(function(a, b) {
return a.name > b.name
});
But it appeared to have no effect on the resulting array when I try to print it out whatsoever.
return (a.name>b.name)-(b.name>a.name)
..sort
interprets0
(AKA+false
) as "equals" – Jan Dvorak Mar 3 '13 at 17:30