Sort day/month array in Javascript

前端 未结 4 1203
南方客
南方客 2021-01-23 12:10

I\'m trying to sort an Array of dates from latest to oldest, and unfortunately list.sort (by default) only sorts the first number. My array looks like this:

var          


        
4条回答
  •  忘掉有多难
    2021-01-23 12:30

    Here's a solution that doesn't reply on any constants.

    var MyArray = ["13 Jun", "09 Jun", "25 Aug", "30 Jun", "13 Aug"];
    var DateArray = [];
    var SortedArray = [];
    var dateBits;
    for (var index = 0; index < MyArray.length; index++) {
     DateArray.push(new Date(MyArray[index] + " 2000"));
    }
    SortedArray = DateArray.sort();
    MyArray = [];
    for (var index = 0; index < SortedArray.length; index++) {
      dateBits = SortedArray[0].toDateString().substr(4, 6).split(" ");
      MyArray[index] = dateBits[1] + " " + dateBits[0];
    }
    

提交回复
热议问题