JavaScript - Sort JS Arrays of Objects by Date Key
Last Updated :
13 Nov, 2024
The following approaches can be used to sort the array of objects by Date as Key
Using Array.sort() with Comparison Function
Use the comparison function to sort the array of objects (complex array).
JavaScript
const arr = [
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'GFG', date: '2022-03-20' },
{ name: 'G4G', date: '2021-03-20' }
];
arr.sort((a, b) => a.date - b.date);
console.log(arr);
Output[
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'GFG', date: '2022-03-20' },
{ name: 'G4G', date: '2021-03-20' }
]
Using JS Date() in Comparison Function
Convert the date string to JS Date Objects.
JavaScript
const arr = [
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'GFG', date: '2022-03-20' },
{ name: 'G4G', date: '2021-03-20' }
];
arr.sort((a, b) => new Date(a.date) - new Date(b.date));
console.log(arr);
Output[
{ name: 'G4G', date: '2021-03-20' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'GFG', date: '2022-03-20' }
]
Using Date.getTime() in Comparison Function
Date.getTime() method returns the time in miliseconds, that can be used to compare and sort he objects.
JavaScript
const arr = [
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'GFG', date: '2022-03-20' },
{ name: 'G4G', date: '2021-03-20' }
];
arr.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
console.log(arr);
Output[
{ name: 'G4G', date: '2021-03-20' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'GFG', date: '2022-03-20' }
]
Using Date.toISOString() method in Comparison Function
Date.toISOString() method converts the date to ISO Format string, we can compare the strings using localeCompare() method to sort the objects.
JavaScript
const arr = [
{ name: 'Geeks', date: new Date('2022-03-15') },
{ name: 'Abc', date: new Date('2022-03-12') },
{ name: 'GFG', date: new Date('2022-03-20') },
{ name: 'G4G', date: new Date('2021-03-20') }
];
arr.sort((a, b) =>
a.date.toISOString().localeCompare(
b.date.toISOString()
));
console.log(arr);
Output[
{ name: 'G4G', date: 2021-03-20T00:00:00.000Z },
{ name: 'Abc', date: 2022-03-12T00:00:00.000Z },
{ name: 'Geeks', date: 2022-03-15T00:00:00.000Z },
{ name: 'GFG', date: 2022-03-20T00:00:00....
Output
[
{ name: 'G4G', date: 2021-03-20T00:00:00.000Z },
{ name: 'Abc', date: 2022-03-12T00:00:00.000Z },
{ name: 'Geeks', date: 2022-03-15T00:00:00.000Z },
{ name: 'GFG', date: 2022-03-20T00:00:00.000Z }
]
Using Moment.js Methods in Comparison Function
Moment.js is a JavaScript library for Date manupulation and formatting. The moment().diff() method is used for comparing the dates in comparison function.
JavaScript
// Import moment.js
const moment = require('moment');
const arr = [
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'GFG', date: '2022-03-20' },
{ name: 'G4G', date: '2021-03-20' }
];
// Sort array by date using moment.js
arr.sort((a, b) => moment(a.date).diff(moment(b.date)));
console.log(arr);
Output
[
{ name: 'G4G', date: '2021-03-20' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'GFG', date: '2022-03-20' }
]
Using dayjs for Date Sorting in Comparison Function
Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates. The dayjs().diff() method can be used to sort arrays of objects by date values efficiently.
JavaScript
// Require dayjs
const dayjs = require('dayjs');
const arr = [
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'GFG', date: '2022-03-20' },
{ name: 'G4G', date: '2021-03-20' }
];
// Sort array by date using dayjs
arr.sort((a, b) => dayjs(a.date).diff(dayjs(b.date)));
console.log(arr);
Output
[
{ name: 'G4G', date: '2021-03-20' },
{ name: 'Abc', date: '2022-03-12' },
{ name: 'Geeks', date: '2022-03-15' },
{ name: 'GFG', date: '2022-03-20' }
]
Similar Reads
How to Sort an Array of Objects Based on a Key in JavaScript ?
In JavaScript, sorting an array of objects based on the key consists of iterating over the array, applying the sort() method or other approaches, and arranging the array in the desired sorting order. Table of Content Using sort() methodUsing Custom Sorting FunctionUsing Lodash _.orderBy() MethodUsin
3 min read
JavaScript - Group Objects by Property into Arrays
Here are the various approaches to grouping objects by property into arrays1. Using a for LoopThe for loop is the most basic and widely used approach.JavaScriptconst a = [ { cat: 'fruit', name: 'apple' }, { cat: 'veg', name: 'carrot' }, { cat: 'fruit', name: 'banana' } ]; const grouped = []; const g
3 min read
How to compare Arrays of Objects in JavaScript?
In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations.Syntax: Before going to detail the comparison techniques, let's first understand h
5 min read
How to Sort JSON Object Arrays Based on a Key?
Sorting is the process of arranging elements in a specific order. In JavaScript, we can sort a JSON array by key using various methods, such as the sort() function. Along with this, we can use various other approaches and methods that are implemented in this article. Below are the approaches to sort
3 min read
How to sort an array of object by two fields in JavaScript ?
We have given an array of objects and the task is to sort the array of elements by 2 fields of the object. There are two methods to solve this problem which are discussed below: Approach 1:First compare the first property, if both are unequal then sort accordingly.If they are equal then do the same
3 min read
JavaScript- Convert an Object to JS Array
Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje
3 min read
Arrays in JavaScript behaves like an Object
In this article, we will learn how you check that javascript arrays behave like objects and why it happens. We will be showing the key and values in the array whether you define the key or not in an array of JavaScript. Reason for Arrays behaving like an object: In JavaScript, there is no real Array
2 min read
How to Sort/Order Keys in JavaScript Objects?
To sort or order keys in JavaScript objects, first get the keys as an array and then apply a sorting mechanism to arrange them.Below are the approaches to sort the keys in JavaScript Objects:Table of ContentUsing forEach() methodUsing reduce methodApproach 1: Using forEach() methodSort the keys of a
2 min read
Sort Array of Objects By String Property Value in JavaScript
Sorting arrays of objects based on a string property can be helpful for handling user data or dynamic lists. Here are different ways to sort an array of Objects By String Property Value.1. Using localeCompare() Method â Most UsedThe JavaScript localeCompare() method returns a number indicating wheth
3 min read
How to Move a Key in an Array of Objects using JavaScript?
The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met
5 min read