0

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.

amiregelz
  • 1,833
  • 7
  • 25
  • 46

2 Answers2

0

You could return a large value, for example

Number.MAX_VALUE
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

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.

amiregelz
  • 1,833
  • 7
  • 25
  • 46