-1

I have array like this:

var arr = [
    ["2013/09/09", 1, 2], 
    ["2013/12/31", 2, 5], 
    ["2014/12/30", 1, 4], 
    ["2013/04/17", 1, 1]
];

I need to sort this array according to date present in each of the sub-arrays.

ex.

var sorted = [
    ["2013/04/17", 1, 1], 
    ["2013/09/09", 1, 2], 
    ["2013/12/31", 2, 5], 
    ["2014/12/30", 1, 4]
];
Rashmin Javiya
  • 5,173
  • 3
  • 27
  • 49
benjamin54
  • 1,280
  • 4
  • 28
  • 48
  • Duplicate of [Sorting objects in an array by a field value in JavaScript](http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript) – Jonathan Lonowski May 29 '14 at 04:40
  • 1
    here is also a link:- http://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript – Ajay2707 May 29 '14 at 04:42
  • See this http://stackoverflow.com/questions/10636779/jquery-date-sorting ==> now in this SO old question people have written Sort for date picker, all you need is to reuse the code `:)` hope this helps. – Tats_innit May 29 '14 at 04:42
  • also: http://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript – Tats_innit May 29 '14 at 04:54
  • 1
    All the possible duplicate suggestions mentioned here are array of objects not multi-dimensional array. The very close one could be the `jquery-date-sorting`. But is still not close enough to replicate the same problem as mentioned in this OP. +1 for non-reasonable downvote – jhyap May 29 '14 at 05:37

3 Answers3

2

Try this,

var arr = [
    ["2013/09/09", 1, 2], 
    ["2013/12/31", 2, 5], 
    ["2014/12/30", 1, 4], 
    ["2013/04/17", 1, 1]
];

var temp;

for(var i=0;i<arr.length;i++)
{
    for(var j=0;j<arr.length;j++)
    {
        var di = new Date(arr[i][0]);
        var dj = new Date(arr[j][0]);

        if(di < dj)
        {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
}
Rashmin Javiya
  • 5,173
  • 3
  • 27
  • 49
2

If you are sorting large arrays, its better to do a date conversion just once, rather than repeating the creation on every jiggle of the sort routine.

    function unaturalSort(arr){
        var T= arr.slice(0), L= T.length, i, itm, next;
        // create a timestamp for each element
        for(i= 0; i<L; i++){
            itm= T[i];
            next= T[i][0];
            T[i]= [+new Date(next), itm];
        }
        T.sort(function(a, b){
            return a[0]-b[0]
        });
        //remove the timestamps from the sorted array
        for(i= 0; i<L; i++){
            T[i]= T[i][1];
        }
        return T;
    }

    var A= [
        ["2013/09/09", 1, 2], ["2013/12/31", 2, 5], 
        ["2014/12/30", 1, 4],["2013/04/17", 1, 1]
    ];


    unaturalSort(A).join('\n'); 

 returned value: (String)
    2013/04/17, 1, 1
    2013/09/09, 1, 2
    2013/12/31, 2, 5
    2014/12/30, 1, 4

Note- skip the slice(), if you mean to rearrange the existing array.

kennebec
  • 102,654
  • 32
  • 106
  • 127
1

Use of native sort and parseInt which I believe I am the most short hand among the other answers:

function sortDate (a,b){
       return parseInt(a[0].split("/").join("")) - parseInt(b[0].split("/").join(""));
}

var arr = [
    ["2013/09/09", 1, 2], 
    ["2013/12/31", 2, 5], 
    ["2014/12/30", 1, 4], 
    ["2013/04/17", 1, 1]
];

var result = arr.sort(sortDate)

console.log(result);
jhyap
  • 3,779
  • 6
  • 27
  • 47