Sort a string date array

五迷三道 提交于 2019-12-28 17:34:50

问题


I want to sort an array in ascending order. The dates are in string format

["09/06/2015", "25/06/2015", "22/06/2015", "25/07/2015", "18/05/2015"] 

Even need a function to check whether these dates are in continuous form:

eg - Valid   - ["09/06/2015", "10/06/2015", "11/06/2015"] 
     Invalid - ["09/06/2015", "25/06/2015", "22/06/2015", "25/07/2015"] 

Example code:

function sequentialDates(dates){
        var temp_date_array = [];

        $.each(dates, function( index, date ) {
            //var date_flag = Date.parse(date);
            temp_date_array.push(date);
        });

        console.log(temp_date_array);

        var last;
        for (var i = 0, l = temp_date_array.length; i < l; i++) {

          var cur = new Date();
          cur.setTime(temp_date_array[i]);
          last = last || cur;
          //console.log(last+' '+cur);

          if (isNewSequence(cur, last)) {
            console.log("Not Sequence");
          }
        }

        //return dates;
    }

     function isNewSequence(a, b) {
          if (a - b > (24 * 60 * 60 * 1000))
              return true;
          return false;
      }

回答1:


The Simple Solution

There is no need to convert Strings to Dates or use RegExp.

The simple solution is to use the Array.sort() method. The sort function sets the date format to YYYYMMDD and then compares the string value. Assumes date input is in format DD/MM/YYYY.

data.sort(function(a,b) {
  a = a.split('/').reverse().join('');
  b = b.split('/').reverse().join('');
  return a > b ? 1 : a < b ? -1 : 0;
  // return a.localeCompare(b);         // <-- alternative 
});

Update:

A helpful comment suggested using localeCompare() to simplify the sort function. This alternative is shown in the above code snippet.

Run Snippet to Test

<!doctype html>
<html>
<body style="font-family: monospace">
<ol id="stdout"></ol>
<script>
  var data = ["09/06/2015", "25/06/2015", "22/06/2015", "25/07/2015", "18/05/2015"];

data.sort(function(a,b) {
  a = a.split('/').reverse().join('');
  b = b.split('/').reverse().join('');
  return a > b ? 1 : a < b ? -1 : 0;
  
  // return a.localeCompare(b);         // <-- alternative 
  
});

for(var i=0; i<data.length; i++) 
  stdout.innerHTML += '<li>' + data[i];
</script>
</body>
</html>



回答2:


You will need to convert your strings to dates, and compare those dates, if you want to sort them. You can make use of the parameter that the sort method accepts, in order to achieve this:

var dateStrings = ["09/06/2015", "25/06/2015", "22/06/2015", "25/07/2015", "18/05/2015"];
var sortedStrings = dateStrings.sort(function(a,b) {
    var aComps = a.split("/");
    var bComps = b.split("/");
    var aDate = new Date(aComps[2], aComps[1], aComps[0]);
    var bDate = new Date(bComps[2], bComps[1], bComps[0]);
    return aDate.getTime() - bDate.getTime();
});

In order to reduce code redundancy, and to handle different date formats, you can add an additional function that will create the comparator needed by the sort method:

function createSorter(dateParser) {
    return function(a, b) {
        var aDate = dateParser(a);
        var bDate = dateParser(b);
        return aDate.getTime() - bDate.getTime();
    };
}

dateStrings.sort(createSorter(function(dateString) {
    var comps = dateString.split("/");
    return new Date(comps[2], comps[1], comps[0]);
}));

You can then use different date formatters by passing different functions to the createSorter call.

As for your second question, you can create an (sorted) array of dates from your strings, and perform your logic on that array:

function myDateParser(dateString) {
    var comps = dateString.split("/");
    return new Date(comps[2], comps[1], comps[0]);
}

var sortedDates = dateStrings.map(myDateParser).sort();

You can walk through the sortedDates array and if you find two non-consecutive dates, then you have dates with gaps between them.




回答3:


var dateRE = /^(\d{2})[\/\- ](\d{2})[\/\- ](\d{4})/;
function dmyOrdA(a, b){
a = a.replace(dateRE,"$3$2$1");
b = b.replace(dateRE,"$3$2$1");
if (a>b) return 1;
if (a <b) return -1;
return 0; }
function dmyOrdD(a, b){
a = a.replace(dateRE,"$3$2$1");
b = b.replace(dateRE,"$3$2$1");
if (a>b) return -1;
if (a <b) return 1;
return 0; }
function mdyOrdA(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; }
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; } 

dateArray = new Array("09/06/2015", "25/06/2015", "22/06/2015", "25/07/2015", "18/05/2015");
var c = dateArray.sort( dmyOrdA );

console.log(c);



回答4:


To sort your date string ascendingly without alteration to its value, try this:

var T = ["09/06/2015", "25/06/2015", "22/06/2015", "25/07/2015", "18/05/2015"];

var sortedT = T.sort(s1,s2){
    var sdate1 = s1.split('/');
    var sdate2 = s2.split('/');
    var date1 = s1[1]+'/'+s1[0]+'/'+s1[2];
    var date2 = s2[1]+'/'+s2[0]+'/'+s2[2];
    if (Date.parse(date1) > Date.parse(date2)) return 1;
    else if (Date.parse(date1) < Date.parse(date2) return -1;
    else return 0;
}

The resultant array sortedT should be a sorted array of date string.

NOTE:

Your date format is stored in dd/mm/yyyy but the standard date format of JavaScript is mm/dd/yyyy. Thus, in order to parse this string to Date without using external date format library, the date string is therefore needed to be converted for compatibility during sort.



来源:https://stackoverflow.com/questions/30691066/sort-a-string-date-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!