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.

1

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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 stackoverflow.com/q/141348/215552.
0

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

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.