{

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


In this tutorial, Learn to sort an Array of numbers, strings, and objects in multiple ways.

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

Sorting Array of dates in javascript

Let’s declare 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 comparator to write a 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 can be written using array functions

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

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 Array of JSON objects with 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'
  }
]

here is an 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)

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 array of different types.

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

I hope you learned new things from this post.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.