I'm using lodash's sortBy to sort an array of objects based on their timestamps (Unix time).
However, I want to set one of the objects to always be last (based on a certain condition).
If I wanted it to be first, I would give it a value of 0
.
let sortedArr = _.sortBy(arr, getSortByKey);
function getSortByKey(item) {
if (item.key === 'lastKey') {
return SORT_BY_KEY_LAST;
}
return item.timestamp;
}
What value should I return there to make lastKey
always be last?
I'm trying to figure out what makes the most sense and is the most readable solution.
Since all of the timestamp
values are known to be in the past, I realized I can use _.now() as a pretty elegant way to set a last sort by timestamp value.