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?
For completeness here is a possible short generic implementation of sortBy:
Note that this uses the arrays sort method that sorts in place. for a copy you can use arr.concat() or arr.slice(0) or similar method to create a copy.
I already answered a really similar question here: Simple function to sort an array of objects
For that question I created this little function that might do what you want:
The Array.sort() method sorts the elements of an array in place and returns the array. Be careful with Array.sort() as it's not Immutable. For immutable sort use immutable-sort.
This method is to sort the array using your current
updated_at
in ISO format. We usenew Data(iso_string).getTime()
to convert ISO time to Unix timestamp. A Unix timestamp is a number that we can do simple math on. We subtract the first and second timestamp the result is; if the first timestamp is bigger than the second the return number will be positive. If the second number is bigger than the first the return value will be negative. If the two are the same the return will be zero. This alines perfectly with the required return values for the inline function.For ES6:
For ES5:
If you change your
updated_at
to be unix timestamps you can do this:For ES6:
For ES5:
At the time of this post, modern browsers do not support ES6. To use ES6 in modern browsers use babel to transpile the code to ES5. Expect browser support for ES6 in the near future.
Array.sort() should receave a return value of one of 3 possible outcomes:
For Immutable sort: (example in ES6)
You can also write it this way:
The import-from you see is a new way to include javascript in ES6 and makes your code look very clean. My personal favorite.
Immutable sort doesn't mutate the source array rather it returns a new array. Using
const
is recommended on immutable data.