2015-08-14 99 views
1

我有下面的日期數組,我想按照擴展順序進行排序。我試過但沒有得到積極的結果。如何在JavaScript中對日期數組進行排序

var arr = [ [ '02/13/2015', 0.096 ], 
    [ '11/15/2013', 0.189 ], 
    [ '05/15/2014', 0.11 ], 
    [ '12/13/2013', 0.1285 ], 
    [ '01/15/2013', 0.12 ], 
    [ '01/15/2014', 0.11 ], 
    [ '02/14/2014', 0.11 ], 
    [ '03/14/2014', 0.11 ], 
    [ '01/15/2015', 0.096 ], 
    [ '07/15/2015', 0.096 ], 
    [ '04/15/2013', 0.12 ], 
    [ '04/15/2014', 0.11 ], 
    [ '05/15/2013', 0.12 ], 
    [ '06/14/2013', 0.12 ], 
    [ '06/16/2014', 0.11 ], 
    [ '07/15/2013', 0.12 ], 
    [ '07/15/2014', 0.11 ], 
    [ '03/16/2015', 0.096 ]] 

我的代碼

arr.sort(function(a,b){ 
    return new Date(a[0][0]) - new Date(b[0][0]); 
}); 
+0

對於初學者來說,'A'是像'陣列['11 /二千零十三分之十五' ,0.189]''這樣一個[0] [0]'將導致'」 1'..這仍然需要工作,但是因爲'new Date(a [0])' - 例如'New Date('11/15/2013')' - 是不明確的行爲。我推薦[Moment.js](http://momentjs.com/)來處理這種情況,但可以手動解析日期並使用'new Date(y,m,d)':沒有短缺這樣的例子當問如何將一個字符串變成日期 – user2864740

+0

如何*「正常」*創建日期,請參閱http://stackoverflow.com/questions/5619202/converting-string-to-date-in-js ,http://stackoverflow.com/questions/22184747/parse-string-to-date-with-moment-js等 – user2864740

回答

7

你正在服用的第一個字符的日期字符串,將它們轉換爲Date實例,並使用它們進行比較。

但是,您應該實際使用日期字符串,將它們轉換爲Date實例並進行比較。

arr.sort(function (a, b) { 
    return new Date(a[0]) - new Date(b[0]); 
}); 

輸出

[ [ '01/15/2013', 0.12 ], 
    [ '04/15/2013', 0.12 ], 
    [ '05/15/2013', 0.12 ], 
    [ '06/14/2013', 0.12 ], 
    [ '07/15/2013', 0.12 ], 
    [ '11/15/2013', 0.189 ], 
    [ '12/13/2013', 0.1285 ], 
    [ '01/15/2014', 0.11 ], 
    [ '02/14/2014', 0.11 ], 
    [ '03/14/2014', 0.11 ], 
    [ '04/15/2014', 0.11 ], 
    [ '05/15/2014', 0.11 ], 
    [ '06/16/2014', 0.11 ], 
    [ '07/15/2014', 0.11 ], 
    [ '01/15/2015', 0.096 ], 
    [ '02/13/2015', 0.096 ], 
    [ '03/16/2015', 0.096 ], 
    [ '07/15/2015', 0.096 ] ] 
+0

FWIW,請參閱我對主帖的評論。將特定於語言環境的字符串傳遞給Date構造函數的錯誤定義的行爲。 – user2864740

0

如果你想只通過日期排序是什麼,我覺得這個代碼將有助於:

arr.sort(function(a,b){return new Date(a[0]) - new Date(b[0]);}); 
1

從MDN Sorting with map重啓發。對於更大的數據集很重要。

var arr = [ 
 
    ['02/13/2015', 0.096], 
 
    ['11/15/2013', 0.189], 
 
    ['05/15/2014', 0.11], 
 
    ['12/13/2013', 0.1285], 
 
    ['01/15/2013', 0.12], 
 
    ['01/15/2014', 0.11], 
 
    ['02/14/2014', 0.11], 
 
    ['03/14/2014', 0.11], 
 
    ['01/15/2015', 0.096], 
 
    ['07/15/2015', 0.096], 
 
    ['04/15/2013', 0.12], 
 
    ['04/15/2014', 0.11], 
 
    ['05/15/2013', 0.12], 
 
    ['06/14/2013', 0.12], 
 
    ['06/16/2014', 0.11], 
 
    ['07/15/2013', 0.12], 
 
    ['07/15/2014', 0.11], 
 
    ['03/16/2015', 0.096] 
 
]; 
 

 
// temporary array holds objects with position and sort-value 
 
var mapped = arr.map(function (el, i) { 
 
    var d = el[0].split('/'); 
 
    return { index: i, value: new Date(d[2], d[0] - 1, d[1]) }; 
 
}) 
 

 
// sorting the mapped array containing the reduced values 
 
mapped.sort(function (a, b) { 
 
    return +(a.value > b.value) || +(a.value === b.value) - 1; 
 
}); 
 

 
// container for the resulting order 
 
var result = mapped.map(function (el) { 
 
    return arr[el.index]; 
 
}); 
 

 
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

+0

爲了歷史的目的,這個月應該是0..11 – user2864740

+0

@ user2864740,你是對的。 –

相關問題