-2

我必须对具有未定义长度(行)的二维数组进行排序以按日期排序。阵列必须具有此构建:

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
}

好的。这给了我一个像这样的多数组(具有未定义的长度/行):

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

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

现在我必须按 Unix 日期对这个多数组(具有未定义的长度/行)进行排序(好吧,这可能是像 YYYYMMDD 这样的正常日期)以降序排列。

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

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

互联网上没有任何帮助,我该怎么办?

非常感谢

4

2 回答 2

2

你需要做这样的事情。

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);

jsfiddle 上

于 2013-08-01T22:13:05.173 回答
1

创建一个函数来比较两个数组元素,例如“使用 JavaScript 比较日期”的答案,然后将您的数组传递给Array 原型中内置的排序例程。

于 2013-08-01T21:36:14.850 回答