0

I got this list of date array that I am trying to sort:

var arr = ['2017/12/16', 
'2017/05/01',
'2017/04/20',
'2017/03/10',
'2017/08/12',
'2017/03/06',
'2017/02/04',
'2017/01/07',
'2016/02/08',
'2015'09/08'];

They are in yyyy/mm/dd format. I tried to use this function to sort:

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

However, it tells me that a.split is not a function.

2
  • 1
    You realize you have a typo in the last date, it's '2015/09/08', while it should be '2015/09/08' after replacing the inner quotation with a slash. Commented Aug 26, 2017 at 11:21
  • 1
    Why do you try convert the date? Just use var sorted = arr.sort(); Commented Aug 26, 2017 at 11:24

2 Answers 2

3

There is no need to convert the strings in your array, you can simply compare strings by using the default behavior of Array#sort:

var arr = [
    '2017/12/16',
    '2017/05/01',
    '2017/04/20',
    '2017/03/10',
    '2017/08/12',
    '2017/03/06',
    '2017/02/04',
    '2017/01/07',
    '2016/02/08',
    '2015/09/08'
];

arr.sort();

console.log(arr);

Sign up to request clarification or add additional context in comments.

5 Comments

Why do you use callback for sort function? arr.sort() is enough.
@PatrickHund Nope it does not work, it is still printing the unsorted array
Did you fix the syntax error in your code? The last date string in the input array has a single quote char in it that doesn't belong there. You can see that my code example works simply by hitting the “Run code snippet” button
btw, sort sorts the given array in situ.
@PatrickHund note that sort works inplace, you don't need to create a new variable for that.
1

Instead of split/reverse/join just convert the string to Date object and let javascript do the sorting for you:

var arr = ['2017/12/16', 
'2017/05/01',
'2017/04/20',
'2017/03/10',
'2017/08/12',
'2017/03/06',
'2017/02/04',
'2017/01/07',
'2016/02/08',
'2015/09/08'
];

arr.sort(function(a, b) {
  da = new Date(a);
  db = new Date(b);
  if (da == db) {
    return 0;
  }
  return da > db ? 1 : -1;
});

console.log(arr);

6 Comments

But strangely my result sorted in descending order
Nope it does not work. No matter I changed the < or > sign, it is still printing the original unsorted array
Without looking at your code that you use it will be a bit hard to help :)
Ah I figured out why is it so already. Because my array was an object array so I needed to do a.date and b.date. Thanks!!!
A few issues with this snipptet, 1) "parsing of date strings with the Date constructor... is strongly discouraged" (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…), 2)da > db is not correct in a sort callback and 3) conversions are simply not needed in this case (see below)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.