3

我有一个格式为的数组

["09-02-2010", " 05-08-2010", "11-11-2010",  "27-09-2010", "10-12-2010", "09-09-2010", "03-09-2010", "13-08-2010", , "11-10-2010","09-06-2010", "08-06-2010", "07-06-2010" ]

我正在尝试根据日期的降序对数组进行排序..

     dateArray.sort( mdyOrdD);

     var dateRE = /^(\d{2})[\/\- ](\d{2})[\/\- ](\d{4})/;

      function mdyOrdD(a, b){
       a = a.replace(dateRE,"$3$1$2");
       b = b.replace(dateRE,"$3$1$2");
       if (a>b) return -1;
       if (a <b) return 1;
       return 0; }

bt 这并没有完全解决我们的问题。可能出了什么问题,还有其他好的方法可以解决这个问题吗?

4

1 回答 1

1

由于您的日期是DD-MM-YYYY格式,并且您希望它们YYYYMMDD按字母顺序排序,请使用

   a = a.replace(dateRE,"$3$2$1");
   b = b.replace(dateRE,"$3$2$1");

反而。

于 2013-04-16T11:17:08.143 回答