Javascript 如何在二维数组中仅对单个列进行排序
我有一个名为dateTime[]的2d数组。dateTime[count][0]包含将来的dateTime,dateTime[count][1]包含一个4位数的值,如1234或其他数字。 我正在尝试按升序对dateTime[count][0]列0进行排序。(即,根据从现在起最接近的日期时间对2d数组的colunm 0进行排序) 假设我的javascript 2d数组如下所示:Javascript 如何在二维数组中仅对单个列进行排序,javascript,multidimensional-array,2d,sorting,Javascript,Multidimensional Array,2d,Sorting,我有一个名为dateTime[]的2d数组。dateTime[count][0]包含将来的dateTime,dateTime[count][1]包含一个4位数的值,如1234或其他数字。 我正在尝试按升序对dateTime[count][0]列0进行排序。(即,根据从现在起最接近的日期时间对2d数组的colunm 0进行排序) 假设我的javascript 2d数组如下所示: dateTime[0][0] = 2/26/2013 11:41AM; dateTime[0][1] = 1234; d
dateTime[0][0] = 2/26/2013 11:41AM; dateTime[0][1] = 1234;
dateTime[1][0] = 2/26/2013 10:41PM; dateTime[1][1] = 4567;
dateTime[2][0] = 2/26/2013 8:41AM; dateTime[2][1] = 7891;
dateTime[3][0] = 3/26/2013 8:41AM; dateTime[3][1] = 2345;
我就是这样写的,实际上这就是我如何将值插入到dateTime[count][0];=新日期(x*1000)代码>其中x是unix时间()
我希望数组在排序后的外观:
dateTime[0][0] = 2/26/2013 8:41AM; dateTime[0][1] = 7891;
dateTime[1][0] = 2/26/2013 11:41AM; dateTime[1][0] = 1234;
dateTime[2][0] = 2/26/2013 10:41PM; dateTime[2][1] = 4567;
dateTime[3][0] = 3/26/2013 8:41AM; dateTime[3][1] = 2345;
请让我知道如何用更少的代码解决这个问题
谢谢。:)
这就是我到目前为止所做的(我还没有对数组进行排序,这里dateTime被称为计时器)
函数checkConfirm(){
var temp=计时器[0][0];
var timeDiv=计时器[0][1];
对于(var i=0;i使用.sort()
函数,并比较日期
// dateTime is the array we want to sort
dateTime.sort(function(a,b){
// each value in this array is an array
// the 0th position has what we want to sort on
// Date objects are represented as a timestamp when converted to numbers
return a[0] - b[0];
});
演示:我已经更新了问题
// dateTime is the array we want to sort
dateTime.sort(function(a,b){
// each value in this array is an array
// the 0th position has what we want to sort on
// Date objects are represented as a timestamp when converted to numbers
return a[0] - b[0];
});