1

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 comes sorted.

3
  • Take a look at this awesome website : sorting-algorithms.com 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. Commented Jun 7, 2012 at 12:36
  • that is an object of objects, not an array of objects. Commented Jun 7, 2012 at 12:42

6 Answers 6

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 compares 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.

Sign up to request clarification or add additional context in comments.

Comments

1
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"
    }
}

Comments

0

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.

Comments

0

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 come out in the same order the second time.

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

Comments

0

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

Working Demo: http://jsfiddle.net/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);​

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.