0

I working with an array of data. Each entry is a post with date (creation date), modification date, title, content, etc.

I want to sort the array with most recent date first, wether it is creation date or modification date, using the date that is available. The specificity is : each post has a creation date but not all posts have a modification date.

I'm no JS expert: I managed to make it work but I want to know if there is a better way to this (I'm sure there is) so I can improve. I worked with the help of this answer and this one.

Here is what I have for now

const values; // array of dated data

// a function that will determine if the element has one or the other date and return the most recent one
function mostRecentDate( obj ) {
    if ( obj.data.date && obj.data.modification ) {
        return Math.max(obj.data.date,obj.data.modification);
    } else if ( obj.data.date && obj.data.modification == null ) {
        return obj.data.date;
    } else if ( obj.data.modification && obj.data.date == null ) {
        return obj.data.modification;
    } else {
        return null;
    }
}

const tempArr = [];
// parse our data
// determine what is the most recent datetime between date or modification
// and build an array where each post has two entries: most recent date and the data
values.forEach(element => {
    tempArr.push([ mostRecentDate(element), element]);
});

// order by most recent date
// a[0] is the most recent date we defined earlier
tempArr.sort(function(a,b) {
    return (a[0] < b[0]) ? 1 : -1
});

// rebuild a simple array of post items
const sorted = [];
tempArr.forEach( item => {
    sorted.push(item[1]);
})
return sorted;

If you want to test it here is a data sample:

const elements = [
    {
        id: '1',
        data: {
            modification: '2022-07-21T18:59:36.641Z',
            date:         '2020-03-17T18:59:36.641Z',
        }
    },
    {
        id: '2',
        data: {
            modification: '2022-07-22T18:59:36.641Z',
            date:         '2022-03-17T18:59:36.641Z',
        }
    },
    {
        id: '3',
        data: {
            modification: '2022-09-22T18:59:36.641Z',
            date:         '2022-09-17T18:59:36.641Z',
        }
    },
    {
        id: '4',
        data: {
            date:           '2022-09-22T18:59:36.641Z',
        }
    },
    {
        id: '5',
        data: {
            date:           '2021-03-15T18:59:36.641Z',
        }
    }
];

example output

[
  { id: '4', data: { date: '2022-09-22T18:59:36.641Z' } },
  { id: '5', data: { date: '2021-03-15T18:59:36.641Z' } },
  {
    id: '3',
    data: {
      modification: '2022-09-22T18:59:36.641Z',
      date: '2022-09-17T18:59:36.641Z'
    }
  },
  {
    id: '2',
    data: {
      modification: '2022-07-22T18:59:36.641Z',
      date: '2022-03-17T18:59:36.641Z'
    }
  },
  {
    id: '1',
    data: {
      modification: '2022-07-21T18:59:36.641Z',
      date: '2020-03-17T18:59:36.641Z'
    }
  }
]
Julien
  • 11
  • 2

0 Answers0