How to Sort a Multidimensional Array in JavaScript by Date ? Last Updated : 13 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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() methodUsing new Date().getTime() methodUsing Date.parse() methodUsing localeCompare() methodThe 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() methodThe 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() methodThe 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' ] ] Comment More infoAdvertise with us Next Article How to Sort a Multidimensional Array in JavaScript by Date ? G gauravggeeksforgeeks Follow Improve Article Tags : JavaScript Web Technologies 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 How to sort an array on multiple columns using JavaScript ? The task is to sort the JavaScript array on multiple columns with the help of JavaScript. There two approaches aree two approaches that are discussed below. Approach 1:Combine the multiple sort operations by OR operator and comparing the strings. For comparing the string, we will use the localeCompa 3 min read How to Sort an Array Based on the Length of Each Element in JavaScript? Imagine you have a list of words or groups of items, and you want to arrange them in order from shortest to longest. This is a pretty common task in JavaScript, especially when working with text or collections of things. By sorting your list in this way, you can make sense of your data and make it e 3 min read How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach 3 min read Sort Mixed Alpha/Numeric array in JavaScript Sorting a mixed alpha/numeric array in JavaScript involves arranging the elements in a specified order, typically lexicographically (dictionary order) for strings and numerically for numbers. When these types are mixed, a custom sort function is required to effectively handle comparisons between dif 3 min read How to sort a set in JavaScript ? Sorting a Set based on the values of its elements is a common need in JavaScript. While the built-in sort() function can't be directly applied to a Set, there's a workaround. Let's explore how we can achieve this: Instead of directly sorting the Set, we'll: Convert the Set into an array to make it s 3 min read Like