Sort object array by more than one comparison in one function [duplicate]

  javascript

I have an array of objects:

const arr = [
  {
    name: 'Kevin',
    timestamp: "1617834779855"
  },
  {
    name: 'Louis',
    timestamp: "1617334735485"
  },
  {
    name: 'Adisson',
    timestamp: "1616530999700"
  },
]

And I need to sort by most recent timestamp and by name in alphabetical order.
Right now I have a sort function to sort by the date but Im not sure how to add the name to the sort function as well

const i = arr.sort((a, b) => b.timestamp - a.timestamp);

My first thought was to add another sort function but I dont want it to loop thru the array again because in my app the array is over 1000 items length

Source: Ask Javascript Questions

LEAVE A COMMENT