How to Sort a Multidimensional Array in JavaScript by Date ?
Last Updated :
13 Feb, 2024
Sorting a Multidimensional Array by date consists of ordering the inner arrays based on the date values. This needs to be done by converting the date representation into the proper comparable formats and then applying the sorting function to sort the array in ascending or descending order.
Below are the possible approaches:
Using localeCompare() method
The localeCompare() method in JavaScript is mainly used for sorting by comparing the string representations in a locale-sensitive manner and ensuring the correct order.
Syntax:
string1.localeCompare(string2, locales, options);
Example: The below code uses the localeCompare() method to sort a multidimensional array in JavaScript by date.
JavaScript
let arr = [
["GeeksforGeeks", "2023-01-15"],
["JavaScript", "2022-05-20"],
["Array", "2022-03-10"]
];
arr.sort((a, b) => a[1].localeCompare(b[1]));
console.log(arr);
Output[
[ 'Array', '2022-03-10' ],
[ 'JavaScript', '2022-05-20' ],
[ 'GeeksforGeeks', '2023-01-15' ]
]
Using new Date().getTime() method
The getTime() method of new Date() converts the data strings into the multidimensional array and performs the numerical comparison by ensuring the correct sorting order is based on the dates.
Syntax:
dateObject.getTime();
Example: The below code uses the new Date().getTime() method to sort a multidimensional array in JavaScript by date.
JavaScript
let arr = [
["GeeksforGeeks", "2023-01-15"],
["JavaScript", "2022-05-20"],
["Array", "2022-03-10"]
];
arr.sort((a, b) =>
new Date(a[1]).getTime() - new Date(b[1]).getTime());
console.log(arr);
Output[
[ 'Array', '2022-03-10' ],
[ 'JavaScript', '2022-05-20' ],
[ 'GeeksforGeeks', '2023-01-15' ]
]
Using Date.parse() method
The Date.parse() method mainly converts the date string into the timestamps. Then it performs the numeric comparison to sort a multidimensional array based on the dates, by creating a sorted array in ascending order of dates.
Syntax:
Date.parse(dateString)
Example: The below code uses the Date.parse() method to sort a multidimensional array in JavaScript by date.
JavaScript
let arr = [
["GeeksforGeeks", "2023-01-15"],
["JavaScript", "2022-05-20"],
["Array", "2022-03-10"]
];
arr.sort((a, b) =>
Date.parse(a[1]) - Date.parse(b[1]));
console.log(arr);
Output[
[ 'Array', '2022-03-10' ],
[ 'JavaScript', '2022-05-20' ],
[ 'GeeksforGeeks', '2023-01-15' ]
]
Similar Reads
How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort
3 min read
How to Select Min/Max Dates in an Array Using JavaScript? Here are the different ways to select min/max dates in an array using JavaScript1. Using Math.max and Math.minUse Math.max and Math.min function to get the maximum and minimum dates respectively.JavaScriptlet dates = [ new Date("2019/06/25"), new Date("2019/06/26"), new Date("2019/06/27"), new Date(
3 min read
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
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
How To Sort An Array Without Mutating The Original Array In JavaScript? Sorting arrays is a common task in JavaScript, especially when you need to present data in a specific order. However, maintaining the original array is often necessary especially if the original data will be required later in your code. In this article we'll see how we can sort an array in JavaScrip
2 min read