Codementor Events

JavaScript story – sorting records by dates

Published Nov 18, 2018
JavaScript story – sorting records by dates

I was looking forward to writing a blog post that I can add into ‘stories’ category. I’d like to tell you what I had to do lately by operating on dates in JavaScript.

My task was to get all of the available records from the database, go through them and sorting them by the submission date. The newest ones had to be first on the list.

1. How did I do it?

I have created a function called sortRecordsByDate. The function was taking one parameter, which is a list of records from the database. It is an array of objects.

function sortRecordsByDate(records) {
    
};

The date format coming from the database was: 12/11/2018, and I had to keep this format for the user. I knew that I want to return a new array of objects immidietaly after mapping through it.

return records.map(record => {
   const dateString = records.submissionDate.split('/').reverse().toString();
   const dateTimestamp = Date.parse(dateString);
   
   record.submissionDate = dateTimestamp;

   return record;
 })

I have chosen to use map, which allows me to return a new array. Let me tell you what is happening inside.
I’m getting ‘submissionDate’ property and splitting it’s value by slash, it converts it to an array, which allows me to use reverse function and convert it to string.

Why would I reverse it? I had to reverse it because when you want to create a new date by using ‘new Date’ constructor, the date format has to be: 2018/11/12.

The next step is to convert our date to the timestamp form of date. In the next step, I’m assigning a new value to a ‘submissionDate’ property. At the end, I’m returning a new record, which contains a new timestamp value instead of the date.

Are we done? Not yet. We still have to sort it.

return records.map(record => {
   const dateString = record.submissionDate.split('/').reverse().toString();
   const dateTimestamp = Date.parse(dateString);

   record.submissionDate = dateTimestamp;

   return record;
 }).sort((a, b) => b.submissionDate – a.submissionDate);

The simple sorting formula allowed me to sort the records from the latest to the oldest by using timestamp for it.
Now, I was having all of the objects sorted in a right way. I was still having a one thing to do though. I had to convert my timestamps to the readable form of date for the user.

.map(record => {
  const submissionDate = new Date(record.submissionDate).toLocaleDateString('en-GB');
  record.submissionDate = submissionDate;

  return record;
 });

I have used map method combined in a chain to bring our old dates by converting the timestamp to the local date string.
The function looks like that:

function sortRecordsByDate(records) {
    return records.map(record => {
         const dateString = record.submissionDate.split('/').reverse().toString();
         const dateTimestamp = Date.parse(dateString);

         record.submissionDate = dateTimestamp;

         return record;
    }).sort((a, b) => b.submissionDate – a.submissionDate).map(record => {
        const submissionDate = new Date(record.submissionDate).toLocaleDateString('en-GB');
        record.submissionDate = submissionDate;

        return record;
   });
};

That would be it, thanks for reading it. If you’ve got any suggestions, please let me know in the comments below.

Discover and read more posts from Robert Wozniak
get started
post commentsBe the first to share your opinion
Show more replies