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 use new 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:
arr.sort((a,b) => new Date(a.updated_at).getTime() - new Date(b.updated_at).getTime());
For ES5:
arr.sort(function(a,b){
return new Date(a.updated_at).getTime() - new Date(b.updated_at).getTime();
});
If you change your updated_at
to be unix timestamps you can do this:
For ES6:
arr.sort((a,b) => a.updated_at - b.updated_at);
For ES5:
arr.sort(function(a,b){
return a.updated_at - b.updated_at;
});
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:
Note that the return value on the inline function can be any positive or negative number. Array.Sort() does not care what the return number is. It only cares if the return value is positive, negative or zero.
For Immutable sort: (example in ES6)
const sort = require('immutable-sort');
const array = [1, 5, 2, 4, 3];
const sortedArray = sort(array);
You can also write it this way:
import sort from 'immutable-sort';
const array = [1, 5, 2, 4, 3];
const sortedArray = sort(array);
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.