Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort a javascript array of objects numerically and then alphabetically? [duplicate]

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"
    }
]
like image 346
Dennis Martinez Avatar asked Oct 15 '12 16:10

Dennis Martinez


People also ask

How do you sort an array of objects alphabetically?

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.

Can you sort an array of objects in JavaScript?

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.


4 Answers

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 ids; 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).

like image 87
raina77ow Avatar answered Oct 14 '22 18:10

raina77ow


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);  
like image 20
bhb Avatar answered Oct 14 '22 17:10

bhb


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.

like image 21
Jeremy J Starcher Avatar answered Oct 14 '22 16:10

Jeremy J Starcher


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;

like image 36
BostonJohn Avatar answered Oct 14 '22 18:10

BostonJohn