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:24Z",
"foo" : "bar"
},
{
"updated_at" : "2012-01-09T11:25:13Z",
"foo" : "bar"
},
{
"updated_at" : "2012-01-05T04:13:24Z",
"foo" : "bar"
}
]
What's the most efficient way to do so?
With ES2015 support it can be done by:
You can use
Array.sort
.Here's an (untested) example:
Data Imported
FOR Ascending order
Example for Asc Order
FOR Descending order
Example for Desc Order
Use underscore js or lodash,
_.sortBy()
returns a new arrayrefer http://underscorejs.org/#sortBy and lodash docs https://lodash.com/docs#sortBy
As for today, answers of @knowbody (https://stackoverflow.com/a/42418963/6778546) and @Rocket Hazmat (https://stackoverflow.com/a/8837511/6778546) can be combined to provide for ES2015 support and correct date handling: