How to Sort an Array of Dates with JavaScript

I recently had to figure out how to sort dates in JavaScript in order to implement chronological sorting in Onpub. I wanted the code to be as simple as possible since I did not want to get in to writing my own sorting algorithm. So I fired up my favorite reference for this kind of stuff and did a little research. It turns out JavaScript has some nifty, built-in functions for dealing with both dates and the sorting of them.

Below is the code I came up with to sort an array of JavaScript Date objects. Feel free to use and adapt this code for your own use. This code should work as expected in all web browsers.

References:

blog comments powered by Disqus