CoderApprentice
CoderApprentice

Reputation: 455

How to sort date strings (format example: 2014 7 23) in JavaScript?

In JavaScript I'm trying to sort an array of objects, each having a date, by date, but I ran into an obstacle. Also the date's are input from 3 dropdown boxes on a site, so I just want 3 numbers. This means I cannot use JavaScript's Date() object, since it also adds a time, timezone and writes month names in letters etc.

Example: I added 5 objects into an array. I have tried using the JavaScript sort function, this one to be specific:

array.sort(function(a, b) {
    if(a.date == b.date){
        return 0;
    }
    else if (a.date < b.date){
        return 1;
    } 
     else {
        return -1;
    }
})

However, this only sorts by year.

So If I add: 2014 7 12 2017 8 16 2017 4 14 2017 1 31 2017 2 26

I get: 2014 7 12 2017 2 26 2017 8 16 2017 1 31 2017 4 14

This is the constructor I use to make different Label objects.

function Label(name, date, type) {
this.name = name;
this.date = date;
this.type = type;

}

var a = new Label("name1", "2016 5 16", 5);
var b = new Label("name2", "2016 7 20", 3);
var c = new Label("name3", "2016 3 15", 2);

My date attributes are just 3 numbers in a string. So I tried rewriting the sort function to this:

    array.sort(function(a,b){
        a = a.date.split(" ");
        b = b.date.split(" ");
        if(a[0] === b[0] && a[1] === b[1] && a[2] === a[2]){
            return 0;
        }
        else if ((a[0] > b[0]) || (a[0] === b[0] && a[1] > b[1]) || (a[0] === b[0] && a[1] === b[1] && a[2] > b[2])){
            return -1;
        } 
        else {
            return 1;
        }
    });

I.e. I tried to use the .split function to seperate the 3 numbers, which are then stored in an array. Date a is then assigned the array with 3 numbers, as is date b. Then I check if the values in both arrays are equal, if so, return 0 (do nothing). If the year in array a is bigger than year in array b, move it down 1 space in the output array. If the year is equal, then check the month. If the month is bigger in a than in b, move a down 1 space in the output array. Etc.

However this doesn't happen. It basically gives me the same output, only sorting by year but ignoring month and day.

I have checked several similar StackOverflow questions, but most of them use the Date() object. I specifically need the format "number number number".

Why doesn't my function work and how might I make it work?

EDIT: made my post a bit clearer with examples of dates and my constructor.

Upvotes: 1

Views: 1201

Answers (3)

adeneo
adeneo

Reputation: 318302

Looks like the date format is always the same as you're already splitting etc. and what you should do is use date objects and compare them instead

array.sort(function(a,b){
    var arr1 = a.date.split(" ");
    var arr2 = b.date.split(" ");

    var time1 = new Date(arr1[0], arr1[1]-1, arr1[2]); // year, month, day
    var time2 = new Date(arr2[0], arr2[1]-1, arr2[2]);

    return time1 - time2;
});

Upvotes: 1

Jamiec
Jamiec

Reputation: 136134

First off, lets just sort a few things out.

  • Javascript Date objects are stored as the number of milliseconds since datum, when you output them to a string they may well have milliseconds, timezone information etc but thats got nothing to do with their internals.
  • Your sort function is an overly complex way of just doing a.date - b.date

Therefore what you want to do is have the properties as actual Date objects, and use the sort function

var sortedArray = array.sort(function(a, b) {
  a.date - b.date;
});

Upvotes: 0

Jonathan Gray
Jonathan Gray

Reputation: 2609

DO use javascript's date object and do the following for sorting:

array.sort(function(a, b) {
    return a.date.getTime()-b.date.getTime();
});

Upvotes: 0

Related Questions