3

For example in my array i have this data

var mydate = [
"2016,10,01",
"2016,09,13", 
"2016,09,05",
"2016,09,09", 
"2016,10,02"];

How to sort this? I want this output:

2016,09,05
2016,09,09
2016,09,13
2016,10,01
2016,10,02
6
  • 1
    There are a number of ways you could do this, super basic example: remove the commas, turn it into an int and use a generic int sort. Commented Oct 5, 2016 at 8:04
  • 2
    mydate.sort() is the least you could do Commented Oct 5, 2016 at 8:04
  • @DBS or don't remove anything and sort the string Commented Oct 5, 2016 at 8:07
  • @mplungjan Fair enough, I didn't think about the string equivalents of numbers working correctly in "alphabetical" order, but that makes sense. Commented Oct 5, 2016 at 8:11
  • stackoverflow.com/questions/30691066/sort-a-string-date-array Commented Oct 5, 2016 at 8:11

4 Answers 4

8

A simple mydate.sort() could do that.

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

11 Comments

w0w nice function I didn't know it exist
Will .sort() not sort based on string values?
@Rajesh Yes and the string "2016,10,02" sorts higher than "2016,10,01" and will sort correctly. It will fail from year "10000,01,01"
@mplungjan it will also fail if OP adds time in string. Also I would not suggest sorting based on string values. Its not a reliable solution
@Tester your sort will fail for "2016, 2, 02" as well. Sample Fiddle
|
2

You will have to parse date into date object and then sort using date.getTime()

var mydate = [
  "2016,10,01",
  "2016,09,13",
  "2016,09,05",
  "2016,09,09",
  "2016,10,02"
];

mydate.sort(function(a,b){
  var da = new Date(a).getTime();
  var db = new Date(b).getTime();
  
  return da < db ? -1 : da > db ? 1 : 0
});
console.log(mydate)

Comments

1

Use Array#sort method with Date constructor.

var mydate = [
  "2016,10,01",
  "2016,09,13",
  "2016,09,05",
  "2016,09,09",
  "2016,10,02"
];

mydate.sort(function(a, b) {
  return new Date(...a.split(',')) - new Date(...b.split(','));
});

console.log(mydate);

Spread syntax not supported by older browser in that case do it like.

var mydate = [
  "2016,10,01",
  "2016,09,13",
  "2016,09,05",
  "2016,09,09",
  "2016,10,02"
];

mydate.sort(function(a, b) {
  var a1 = a.split(','),
    b1 = b.split(',');
  return new Date(a1[0], a1[1], a1[2]) - new Date(b1[0], b1[1], b1[2]);
});

console.log(mydate);

3 Comments

can you explain Date(...a.split(','))
@sheetal : which helps to treat the array value as it's argument
0

var mydate = [
  "2016,10,01",
  "2016,09,13",
  "2016,09,05",
  "2016,09,09",
  "2016,10,02"
];

mydate.sort(function(a,b){
  var da = new Date(a).getTime();
  var db = new Date(b).getTime();
  
  return da - db
});
console.log(mydate)

Comments

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.