-3

I've this Array of objects:

[
  {
    id: 1,
    timestamp1: Date,
    timestamp2: Date,
    timestamp3: Date
 },
 {
    id: 2,
    timestamp1: Date,
    timestamp2: Date,
    timestamp3: Date
 }.
 ...
]

I want to sort the array by newest Date comparing all 3 object properties and using the newest one for each object.

Tank you very much in advance.

3 Answers3

2

By looking at the js sort documentation you'll see that sort is taking a function as parameter which :

  • Return a positive integer if the first object is bigger than the second one
  • Return 0 if the two objects are the same
  • Return a negative integer if the first object is smaller than the second one.

Knowing that, let's build the function together :

First, we'd like to get the highest date for on object.

For that, i would recommend using Math.max which takes an array and returns the biggest parameters.

Here it works because js understand date as integers.

function highestDate(obj){
   return Math.max(obj.timestamp1,obj.timestamp2,obj.timestamp3)
}

Now let's build the sort function.

function compare(a,b){
   return highestDate(b) - highestDate(a)
}

here is a snipper for testing :

function highestDate(obj){
   return Math.max(obj.timestamp1,obj.timestamp2,obj.timestamp3)
}


function compare(a,b){
   return highestDate(b) - highestDate(a)
}

let obj1={
  id:1,
  timestamp1 : new Date(2001,1,1),
  timestamp2 : new Date(2002,1,1),
  timestamp3 : new Date(2003,1,1) //third highest date
}

let obj2={
  id:2,
  timestamp1 : new Date(1997,1,1),
  timestamp2 : new Date(2004,1,1),//second highest date
  timestamp3 : new Date(2003,1,1) 
}

let obj3={
  id:3,
  timestamp1 : new Date(1991,1,1),
  timestamp2 : new Date(2001,1,1),
  timestamp3 : new Date(2005,1,1) //highest date 
}

let arr = [obj1,obj2,obj3]

console.log(arr.sort(compare))
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
RenaudC5
  • 3,553
  • 1
  • 11
  • 29
1

We can find the maximum timestamp for each object using Object.values and Math.Max.

Creating a function to find these values getMaxTimestamp, we can use this in array.sort() to sort the objects by the maximum timestamp:

 
const arr = [ { id: 1, timestamp1: new Date('2021-07-01T16:00:00Z'), timestamp2: new Date('2021-03-01T08:00:00Z'), timestamp3: new Date('2018-10-08T19:00:00Z') }, { id: 2, timestamp1: new Date('2019-08-01T16:00:00Z'), timestamp2: new Date('2015-09-21T08:00:00Z'), timestamp3: new Date('2017-10-08T19:00:00Z') }, { id: 3, timestamp1: new Date('2024-01-01T16:00:00Z'), timestamp2: new Date('2015-09-21T08:00:00Z'), timestamp3: new Date('2021-10-08T19:00:00Z') } ]

function getMaxTimestamp(obj) {
    const timestamps = Object.values(obj).filter(v => v instanceof Date);
    return Math.max(...timestamps);
}

const result = arr.sort((a,b) => { 
    return getMaxTimestamp(a) - getMaxTimestamp(b);
})

console.log("Result:",result)
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
0

Just use Array.sort() and try the comparison functions in this question. There would be some tweaking involved while finding the max date should be easy to do if you just look around with a simple google search.

r8tgcrla
  • 66
  • 1
  • 1
  • 8