Possible Duplicate:
Sorting objects in an array by a field value in JavaScript
How can I sort an array of objects numerically (by id) then alphabetically (by name)?
The current way is providing invalid output.
This is the object i'm trying to sort through
var items = [
{
"id": 165,
"name": "a"
},
{
"id": 236,
"name": "c"
},
{
"id": 376,
"name": "b"
},
{
"id": 253,
"name": "f"
},
{
"id": 235,
"name": "e"
},
{
"id": 24,
"name": "d"
},
{
"id": 26,
"name": "d"
}
]
and the way i'm trying to sort
items.sort(function(a, b) {
return (a.id - b.id);
}).sort(function(a, b) {
return (a.name - b.name);
});
here is the jsfiddle.
EDIT: Sorry for the confusion, I've been so confused by this problem for a while.
What I'm trying to accomplish is to sort by the highest id first, and then sort alphabetically so in the end it should look like:
var items = [
{
"id": 376,
"name": "b"
},
{
"id": 253,
"name": "f"
},
{
"id": 236,
"name": "c"
},
{
"id": 235,
"name": "e"
},
{
"id": 165,
"name": "a"
},
{
"id": 26,
"name": "d"
},
{
"id": 24,
"name": "d"
}
]