I have an array of objects with several key value pairs, and I need to sort them based on \'updated_at\':
[
{
\"updated_at\" : \"2012-01-01T06:25
Just another, more mathematical, way of doing the same thing but shorter:
arr.sort(function(a, b){
var diff = new Date(a.updated_at) - new Date(b.updated_at);
return diff/(Math.abs(diff)||1);
});
or in the slick lambda arrow style:
arr.sort((a, b) => {
var diff = new Date(a.updated_at) - new Date(b.updated_at);
return diff/(Math.abs(diff)||1);
});
This method can be done with any numeric input