Take the 2-minute tour ×

how to sort multi dimensional array in javascript

        how to sort this multi dimensional array in javascript

            var arr = [];
            arr[0]  = [{id:1}, {id:5}, {id:7}];
            arr[1] =  [{id:3}, {id:8}, {id:6}];
            arr[2] = [{id:2}, {id:4}, {id:9}];

            The output should be like this
            arr[0]  = [{id:1}, {id:2}, {id:3}];
            arr[1] =  [{id:4}, {id:5}, {id:6}];
            arr[2] = [{id:7}, {id:8}, {id:9}];
share|improve this question
And where is your effort? –  Sandip Armal Patil Oct 10 '12 at 8:02
Try to go through this sort documentation w3schools.com/jsref/jsref_sort.asp and then ask any problem you encountered. –  AurA Oct 10 '12 at 8:04
Since Javascript's Array object is unidimensional, there's no built-in method to do what you intend. As a matter of fact, you are not sorting, you are moving elements between completely separate array objects. –  Gerardo Lima Oct 10 '12 at 8:06

3 Answers 3

The easiest solution I can think of is this:

var sortChunks = function(ar, property,chunkSize)
    //set default params:
    chunkSize = chunkSize || ar[0].length;// || 3?;
    property = property || 'id';
    var i=0,j=0,all = [],ret = [];
    {//"map" objects, use property as array index
            all[ar[i][j][property]] = ar[i][j];
    {//move all undefined keys to the end of the array
        if (a === undefined)
            return 1;
        if (b === undefined || b.id > a.id)
            return -1;
            return 1;
    while(all && all[0] !== undefined)
    {//make chunks
    return ret;
arr = sortChunks(arr);//returns desired array
arr = sortChunks(arr,'id',3);

Having said that, you're probably better of rethinking the way this array is built, and change that code, rather than relying on this rather meaty (and in its current form rather error-prone) sorting function.

Meanwhile, check this fiddle to see it in action... sort of

share|improve this answer

You will need some helper functions:

Array.prototype.flattened = function() {
    return Array.prototype.concat.apply([], this);
Array.prototype.splitBy = function(n) {
    var r = [];
    for (var i=0; i<this.length; i+=n)
        r.push(this.slice(i, i+n));
    return r;
Array.prototype.sortBy = function sortBy(key) {  
if (this[0] && this[0][key] == "number")
        return this.sort(function(a,b) {return a[key] - b[key];});
    return this.sort(function(a, b) {
        return a[key]<b[key] ? -1 : a[key]>b[key] ? 1 : 0;

Then, it is easy:


(Demo at jsfiddle.net)

share|improve this answer
+1, a little dense, but a more generic approach all round. To the OP: read up on the possible consequences of augmenting prototypes, though... not that this code will cause major conflicts, but just know why you shouldn't go prototype-crazy all of a sudden. –  Elias Van Ootegem Oct 10 '12 at 9:50

You can create "1 dimensional" array with n * m elements. where n and m are number of rows and columns. Then use one of sorting algorithms.


share|improve this answer

Your Answer


