我必须将具有未定义长度(行)的2D数组排序为按日期排序。数组必须有这个版本:
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-Date(这可能是正常的日期,如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
......
......
......
......
......
互联网上什么都没有帮助我,我必须做什么?
非常感谢
答案 0 :(得分: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);
上
答案 1 :(得分:1)
创建一个函数来比较两个数组元素,比如“Compare dates with JavaScript”的答案,然后将数组传递给Array原型中内置的sort routine。