-3

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

0

2 Answers 2

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

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

5 Comments

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 It does help, if you would pay close attention to what it does.
@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. You just need to replace a[1] with a[0] and b[1] with b[0].
@der Wut? slice makes a copy for demonstration purposes.
"a to long answer"? The OP doesn't get to specify the length of answers.
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.

1 Comment

that does not help me? i have to sort an 2D-Array with undefined length (rows)

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.