Multiple ways to sort array JSON object with key date field in javascript

Sort an Array of numbers, strings, and objects in multiple ways.

In my previous post, You learned how to javascript sort Array.

  • Array sort() method sorts the elements in ascending by default. use callback custom functions to have a custom order for properties.
  • Es6 sort method with lambda expression

Sorting Array of dates in javascript

Let’s declare an array of Dates.

var arrayJson = [
  "2011-01-01 02:00:00",
  "2019-01-01 02:00:00",
  "2001-01-01 02:00:00",
  "2021-01-01 02:00:00",
  "2020-01-01 02:00:00",
  "2020-02-01 02:00:00",
  "1990-01-01 02:00:00",
];

Array’s sort() method sorts the array in ascending order by default. It uses a callback custom comparator to write custom logic.

array.sort() method sort the array in ascending order

let result = arrayJson.sort(function (a, b) {
  return new Date(a).getTime() - new Date(b).getTime();
});
console.log(result);

And the output is

[
  '1990-01-01 02:00:00',
  '2001-01-01 02:00:00',
  '2011-01-01 02:00:00',
  '2019-01-01 02:00:00',
  '2020-01-01 02:00:00',
  '2020-02-01 02:00:00',
  '2021-01-01 02:00:00'
]

if you want an array of dates in reverse order i.e. descending. You have to use a sort comparator. Let’s sort the numbers in reverse order i.e. descending.

let result = arrayJson.sort(function (a, b) {
  return new Date(b).getTime() - new Date(a).getTime();
});
console.log(result);

And the output is

[
  "2021-01-01 02:00:00",
  "2020-02-01 02:00:00",
  "2020-01-01 02:00:00",
  "2019-01-01 02:00:00",
  "2011-01-01 02:00:00",
  "2001-01-01 02:00:00",
  "1990-01-01 02:00:00",
];

Sort Array of dates with ES6

The above example uses ES5 code array sort callbacks.

using ES6, a Callback comparator lambda expression can be written using array functions

With Es6(ECMAScript2015), The same can be rewritten using arrow functions as follows

The code for Sorting the array in ascending order is

let result = arrayJson.sort(
  (a, b) => new Date(a).getTime() - new Date(b).getTime(),
);

sorting the date array in descending order using the following

let result = arrayJson.sort(
  (a, b) => new Date(b).getTime() - new Date(a).getTime(),
);

How to Sort an Array of JSON objects with a Date key in Javascript

Let’s take this as an example

[
  {
    "id": 1,
    "name": "john",
    "lastModified": "2011-01-01 02:00:00"
  },
  {
    "id": 2,
    "name": "Franc",
    "lastModified": "2001-01-01 02:00:00"
  },
  {
    "id": 3,
    "name": "Andrew",
    "lastModified": "2021-01-01 02:00:00"
  },
  {
    "id": 11,
    "name": "Mark",
    "lastModified": "2020-01-01 02:00:00"
  },
  {
    "id": 12,
    "name": "Eric",
    "lastModified": "2020-02-01 02:00:00"
  },
  {
    "id": 8,
    "name": "Tony",
    "lastModified": "1990-01-01 02:00:00"
  }
]

The object contains properties, lastModified property is a Date type.

Here is a code for Sort JSON array with date key in ascending order

let result = arrayJson.sort(
  (a, b) =>
    new Date(a.lastModified).getTime() - new Date(b.lastModified).getTime(),
);

console.log(result);

To sort in Descending order

let result = arrayJson.sort(
  (a, b) =>
    new Date(b.lastModified).getTime() - new Date(a.lastModified).getTime(),
);

console.log(result);

Conclusion

In this post, You learned how to sort an different types of an array.

  • Array of dates sorted in ascending or descending order with ES6 and ES5
  • object array json with a key date sort