How to use Date.parse() method In Javascript
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 = [ [ "w3wiki" , "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); |
[ [ 'Array', '2022-03-10' ], [ 'JavaScript', '2022-05-20' ], [ 'w3wiki', '2023-01-15' ] ]
How to Sort a Multidimensional Array in JavaScript by Date ?
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:
Table of Content
- Using localeCompare() method
- Using new Date().getTime() method
- Using Date.parse() method