admin管理员组

文章数量:1392002

I have an array of objects like the following :

var array = {
    "112" : {
             "id": "3",
             "name": "raj"
    },
    "334" : {
             "id": "2",
             "name": "john"
    },
    "222" : {
             "id": "5",
             "name": "kelvin"
    }
}

Now i want to sort the array in ascending order of id and then restore it in array. I tried using sort() but could not do it. Please help how to do so that when i display the data from the array it es sorted.

I have an array of objects like the following :

var array = {
    "112" : {
             "id": "3",
             "name": "raj"
    },
    "334" : {
             "id": "2",
             "name": "john"
    },
    "222" : {
             "id": "5",
             "name": "kelvin"
    }
}

Now i want to sort the array in ascending order of id and then restore it in array. I tried using sort() but could not do it. Please help how to do so that when i display the data from the array it es sorted.

Share Improve this question edited Jun 7, 2012 at 12:41 user850234 asked Jun 7, 2012 at 12:29 user850234user850234 3,46315 gold badges52 silver badges84 bronze badges 3
  • Take a look at this awesome website : sorting-algorithms. – Alexandre Khoury Commented Jun 7, 2012 at 12:33
  • There are syntax errors in your code, please fix them. 112 = should be 122: , whyle raj and the others should be string, unless they're variables, but I guess they aren't. – MaxArt Commented Jun 7, 2012 at 12:36
  • that is an object of objects, not an array of objects. – jbabey Commented Jun 7, 2012 at 12:42
Add a ment  | 

6 Answers 6

Reset to default 1

Assuming you meant your code to be an array of objects, ie:

var unsortedArray = [
    { id: 3, name: "raj" },
    { id: 2, name: "john" },
    { id: 5, name: "kelvin" } 
];

Then you would be able to sort by id by passing a function to Array.sort() that pares id's:

var sortedArray = unsortedArray.sort(function(a, b) {
    return a.id - b.id 
});

As others have pointed out, what you have is an object containing objects, not an array.

var array = {
    "112" : {
         "id": "3",
         "name": "raj"
    },
    "334" : {
         "id": "2",
         "name": "john"
    },
    "222" : {
         "id": "5",
         "name": "kelvin"
    }
}
var sortedObject = Array.prototype.sort.apply(array);

result:
{
    "112": {
        "id": "3",
        "name": "raj"
    },
    "222": {
        "id": "5",
        "name": "kelvin"
        },
    "334": {
        "id": "2",
        "name": "john"
    }
}

That isn't an array, it is an object (or would it if it wasn't for the syntax errors (= should be :)). It doesn't have an order.

You could use an array instead (making the current property names a value of a key on the subobjects).

Alternatively, you could use a for loop to build an array of the key names, then sort that and use it as a basis for accessing the object in order.

JavaScript objects are unordered by definition. The language specification doesn't even guarantee that, if you iterate over the properties of an object twice in succession, they'll e out in the same order the second time.

If you need things to be ordered, use an array and the Array.prototype.sort method.

That is an object but you can sort an array ilke this:

Working Demo: http://jsfiddle/BF8LV/2/

Hope this help,

code

   function sortAscending(data_A, data_B)
{
    return (data_A - data_B);
}
var array =[ 9, 10, 21, 46, 19, 11]
array.sort(sortAscending) 
alert(array);​

Not many people knows that Array.sort can be used on other kinds of objects, but they must have a length property:

array.length = 334;
Array.prototype.sort.call(array, function(a, b) {return a.id - b.id;});

Unfortunately, this doesn't work well if your "array" is full of "holes" like yours.

本文标签: javascriptHow to sort an array of objects in ascending order of numberStack Overflow