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.
http://jsfiddle.net/jh4xb/
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"
}
]
Sort Array of Objects Alphabetically Using the if Condition and sort() Function in JavaScript. If we have an array of strings or integers, we can easily sort them using the sort() function in JavaScript. For example, let's sort an array of strings alphabetically using the sort() function.
Sort an Array of Objects in JavaScript To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.
I think it's better done just with...
items.sort(function(a, b) {
return a.id - b.id || a.name.localeCompare(b.name);
});
The second sort will basically negate the first one, so you have to do it once. )
a.id - b.id || a.name.localeCompare(b.name)
expression will first compare the id
s; only if they are equal, it will compare the names (and return the result of this comparison).
If you need to reverse the ordering, swap the positions (b.id - a.id
, etc.) - or just negate the whole thing:
items.sort(function(a, b) {
return - ( a.id - b.id || a.name.localeCompare(b.name) );
});
Here's the JSFiddle (have to simplify the data a bit to show my point).
Create a general function to sort it anyway you want. Check the dynamicSort function below
var items = [
{
"id": 165,
"name": "a"},
{
"id": 236,
"name": "b"},
{
"id": 376,
"name": "c"},
{
"id": 253,
"name": "d"},
{
"id": 235,
"name": "e"},
{
"id": 24,
"name": "f"}
];
function dynamicSort(property) {
return function(a, b) {
return (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
}
}
items.sort(dynamicSort('name')).sort(dynamicSort('id'));
console.log(items);
items.sort(dynamicSort('id')).sort(dynamicSort('name'));
console.log(items);
IF you mean that you wanted them sorted by id
and if the id
matches, you want them sorted by name
then use this:
items.sort(function(a, b) {
if (a.id !== b.id) {
return a.id - b.id
}
if (a.name === b.name) {
return 0;
}
return a.name > b.name ? 1 : -1;
});
Otherwise your question is unclear.
your problem is how you're sorting the letters.a.name - b.name
isn't doing what you think. It's not evaluating them in lexigraphical order. replace it with
a.name < b.name ? -1 : 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With