-2

i have to sort an 2D-Array with undefined length (rows) to Sort by Date. The Array must have this build:

var Data = new Array();
var ArrayCount = 0;

for(var i = 0; i < CSVDataRow.length; ++i) {

    CreditCardData[ArrayCount] = new Array();

    Data[ArrayCount][0] = new Date(YYYY,MM,DD); // Date
    Data[ArrayCount][1] = amount; // Example for Money
    Data[ArrayCount][2] = purpose1; // Example for Text
    Data[ArrayCount][3] = purpose2; // Example for Text
    Data[ArrayCount][4] = purpose3; // Example for Text

    ArrayCount = ArrayCount +1; // Count for the next Array
}

OK. This give me an Multi-Array (with undefined length/rows) like this:

Data[0][0] = Tue Jul 30 2013 00:00:00 GMT+0200 (MESZ)
Data[0][1] = 200.00
Data[0][2] = Example Text 02
Data[0][3] = Example Text 03
Data[0][4] = Example Text 04

Data[1][0] = Tue Jul 09 2013 00:00:00 GMT+0200 (MESZ)
Data[1][1] = 500.00
Data[1][2] = Example Text 12
Data[1][3] = Example Text 13
Data[1][4] = Example Text 14

Data[2][0] = Tue Jul 15 2013 00:00:00 GMT+0200 (MESZ)
Data[2][1] = 333.00
Data[2][2] = Example Text 22
Data[2][3] = Example Text 23
Data[2][4] = Example Text 24

Data[3][0] = Mon Jul 02 2013 00:00:00 GMT+0200 (MESZ)
Data[3][1] = 777.00
Data[3][2] = Example Text 32
Data[3][3] = Example Text 33
Data[3][4] = Example Text 34

......
......
......
......
......

Now i must sort this Multi-Array (with undefined length/rows) by the Unix-Date (ok its possible that this is a normal Date like YYYYMMDD) in descending order.

Data[0][0] = Tue Jul 30 2013 00:00:00 GMT+0200 (MESZ)
Data[0][1] = 200.00
Data[0][2] = Example Text 02
Data[0][3] = Example Text 03
Data[0][4] = Example Text 04

Data[1][0] = Tue Jul 15 2013 00:00:00 GMT+0200 (MESZ)
Data[1][1] = 333.00
Data[1][2] = Example Text 22
Data[1][3] = Example Text 23
Data[1][4] = Example Text 24

Data[2][0] = Tue Jul 09 2013 00:00:00 GMT+0200 (MESZ)
Data[2][1] = 500.00
Data[2][2] = Example Text 12
Data[2][3] = Example Text 13
Data[2][4] = Example Text 14

Data[3][0] = Mon Jul 02 2013 00:00:00 GMT+0200 (MESZ)
Data[3][1] = 777.00
Data[3][2] = Example Text 32
Data[3][3] = Example Text 33
Data[3][4] = Example Text 34

......
......
......
......
......

Nothing in Internet help me, what i have to do?

Many Thanks

gherkins
  • 14,603
  • 6
  • 44
  • 70
derRichter
  • 13
  • 1
  • 5

2 Answers2

2

You will need to do something like this.

Javascript

var data = [],
    sortThis,
    i;

for (i = 0; i < 10; i += 1) {
    data[i] = [];
    data[i][0] = new Date(2013, 08, 30 - i).getTime() / 1000; // Unix-Date
    data[i][1] = i * 100; // Example for Money
    data[i][2] = "text1 " + i; // Example for Text
    data[i][3] = "text2 " + i; // Example for Text
    data[i][4] = "text3 " + i; // Example for Text
}

sortThis = data.slice();
sortThis.sort(function (a, b) {
    if (a[0] === b[0]) {
        return 0;
    }

    if (a[0] < b[0]) {
        return -1;
    }

    return 1;
});

console.log(data, sortThis);

On jsfiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • that does not help me? i have to sort an 2D-Array with undefined length (rows) and i have to build exaclty like my question – derRichter Aug 02 '13 at 06:31
  • 2
    @derRichter It *does* help, if you would pay close attention to what it does. – deceze Aug 02 '13 at 06:53
  • @deceze this is a to long answer and why slice? why a new build from my array? oh .... this is a good answer look at this answer [link](http://stackoverflow.com/questions/2793847/sort-outer-array-based-on-values-in-inner-array-javascript/2793880#2793880). You just need to replace a[1] with a[0] and b[1] with b[0]. – derRichter Aug 02 '13 at 07:24
  • 1
    @der Wut? `slice` makes a copy for demonstration purposes. – deceze Aug 02 '13 at 07:25
  • "a to long answer"? The OP doesn't get to specify the length of answers. – the Tin Man Aug 02 '13 at 16:03
1

Create a function to compare the two array elements, like the answer to "Compare dates with JavaScript", and then pass your array to the sort routine built into the Array prototype.

Community
  • 1
  • 1
corsiKa
  • 81,495
  • 25
  • 153
  • 204