Skip to content

Instantly share code, notes, and snippets.

Created February 8, 2012 20:15
Show Gist options
  • Save onpubcom/1772996 to your computer and use it in GitHub Desktop.
Save onpubcom/1772996 to your computer and use it in GitHub Desktop.
How to Sort an Array of Dates with JavaScript
<script type="text/javascript">
// First let's create an array of JavaScript Date
// objects.
// More info about the Date class:
var dates = [
new Date(2010, 4, 10, 10, 07, 16),
new Date(2010, 4, 8, 9, 16, 09),
new Date(2010, 3, 30, 0, 15, 49),
new Date(2010, 3, 8, 10, 08, 35)];
// Now we will define our date comparison functions. These are callbacks
// that we will be providing to the array sort method below.
var date_sort_asc = function (date1, date2) {
// This is a comparison function that will result in dates being sorted in
// ASCENDING order. As you can see, JavaScript's native comparison operators
// can be used to compare dates. This was news to me.
if (date1 > date2) return 1;
if (date1 < date2) return -1;
return 0;
var date_sort_desc = function (date1, date2) {
// This is a comparison function that will result in dates being sorted in
// DESCENDING order.
if (date1 > date2) return -1;
if (date1 < date2) return 1;
return 0;
// Finally, we are now able to call the sort method on our array of dates.
// More info about array sorting:
// First let's sort the array in ascending order.
// Now let's output the results to the page to show that the dates are now
// sorted in ascending order.
document.write('<p>Dates sorted in ascending order (oldest to newest):</p>');
for (var i = 0; i < dates.length; i++) {
document.write(i + ': ' + dates[i] + '<br>');
// Now let's sort the dates in descending order and output the results.
document.write('<p>Dates sorted in descending order (newest to oldest):</p>');
for (var i = 0; i < dates.length; i++) {
document.write(i + ': ' + dates[i] + '<br>');
// That's all there is to it!
// From:
Copy link

on @onpubcom your code work great :) thank you .
@saich only sort() will not work.

Copy link

devtanc commented Aug 21, 2016

@saich Array.prototype.sort() will not work as you're hoping. The MDN docs state that if you omit a custom sort function:

The array is sorted according to each character's Unicode code point value, according to the string conversion of each element.

This means that each date will be toString-ed to a date string such as Sun Aug 21 2016 00:00:00 GMT-0600 (MDT) first, then each character, starting from the left, will have it's Unicode point value compared one at a time. This results in unexpected behavior in the sort, since it will essentially sort the array by days of the week first instead of absolute time of each date.

The < and > operators call the Object.prototype.valueOf() function for each operand, then compare the values. This means that a < or > comparison between two dates calls the Date.prototype.valueOf() function for each date object before comparing the values, giving an accurate, absolute comparison.

Copy link

probably late but also a simple subtraction of data prototypes to sort function would work just fine , reduce unnecessary return -1 like :

var date_sort_asc = function (date1, date2) {
  // Accending order.
return date2 - date1

even better es6 one-liner:
dataArray.sort((a,b)=> b-a)

Copy link

jetobe95 commented Jun 4, 2018


Copy link

@obonyojimmy that is not working in my environment. Node.js version 9.

Copy link

siddhant1 commented Mar 3, 2019

Won't work because it returns NaN , compare and return yourself @lancedolan

Copy link

I think @obonyojimmy 's functions actually sort in descending order. Switch to dateArray.sort((a,b)=> a-b) for ascending.

date console

Copy link

For anyone who comes across this looking from a TS compliant approach.

dataArray.sort((a,b)=> Number(a)-Number(b))


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment