Sorting Dates in javascript
By Anatoly Mironov
To sort an array in javascript, just run sort():
array = \["skåne", "blekinge", "halland"\];
array.sort();
```To [perform a more intelligent sorting](http://stackoverflow.com/questions/3859239/sort-json-by-date "See the original answer in stackoverflow") we can define our sorting logic. What if an array has objects with different properties? For example, an object has title and lastupdated. We want to sort on lastupdated. Just create a function for this with two parameters:
function custom_sort(a, b) { return new Date(a.lastUpdated).getTime() - new Date(b.lastUpdated).getTime(); }
var dates = [ {title: “Narspi”, lastUpdated: “2010-01-01”}, {title: “Setner”, lastUpdated: “2009-01-01”}, {title: “Mikheder”, lastUpdated: “2010-07-01”} ];
dates.sort(custom_sort);
[![](https://sharepointkunskap.files.wordpress.com/2011/11/sorting-dates.png "sorting-dates")](https://sharepointkunskap.files.wordpress.com/2011/11/sorting-dates.png)