1

Would like to sort an array of form ["01:00 am","06:00 pm" ,"12:00 pm","03:00 am","12:00 am"]

Please suggest.

Leri
  • 12,367
  • 7
  • 43
  • 60
BiswajitP
  • 233
  • 4
  • 15

2 Answers2

2
var times = ['01:00 am', '06:00 pm', '12:00 pm', '03:00 am', '12:00 am'];

times.sort(function (a, b) {
  return new Date('1970/01/01 ' + a) - new Date('1970/01/01 ' + b);
});

console.log(times);
Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • Note that in 2017, this is actually a bad idea, as calling the Date constructor with a string that's anything other than an ISO 8601-formatted date will not work cross-browser. Better to use the solutions outlined in https://stackoverflow.com/q/141348/215552. – Heretic Monkey May 22 '17 at 17:20
0

How about taking a look at Underscore.js SortBy? http://underscorejs.org/#sortBy

soliloquyy
  • 355
  • 3
  • 14