Sort An Array Of Arrays In JavaScript Last Updated : 15 Nov, 2024 Comments Improve Suggest changes Like Article Like Report The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order. JavaScript let arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr); Output[ [ 1, 4 ], [ 2, 0 ], [ 2, 5 ], [ 3, 2 ] ] By default it sorts the element by first index. In case, first elements are equal, it compares the following elements and sort accordingly.Sorting by a Specific ColumnTo sort an array of arrays by a specific column, you need to provide a custom comparator function to sort() that specifies which index in the sub-arrays should be compared. JavaScript let arr = [[3, 2], [1, 4], [2, 5], [5, 1]]; arr.sort((a, b) => a[1] - b[1]); console.log(arr); Output[ [ 5, 1 ], [ 3, 2 ], [ 1, 4 ], [ 2, 5 ] ] 2. Using Bubble Sort AlgorithmBubble Sort is a simple comparison based algorithm where adjacent elements are compared and they are swapped if they are in the wrong order. This process repeats for all elements in the array until it is fully sorted.You can refer to this article: Bubble sort for Array sorting3. Using Selection Sort AlgorithmSelection Sort works by finding the smallest (or largest) element in the array and moving it to its correct position. This repeats for each position in the array until it is sorted.You can refer to this article: Selection sort for Array sorting4. Using Insertion Sort AlgorithmInsertion Sort is a simple sorting algorithm where each element is placed in its correct position relative to the elements already sorted. It works by gradually building up a sorted portion of the array.You can refer to this article: Insertion sort for Array sorting Comment More infoAdvertise with us Next Article Sort An Array Of Arrays In JavaScript jaimin78 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads Sort an Array in JavaScript This article will show you how to sort a JS array.1. Using array.sort() Method To sort an array in JavaScript, we can use array.sort() method. This method sorts the elements alphabetically in ascending order.JavaScriptlet arr = ["JS", "HTML", "CSS"]; arr.sort() console.log(arr);Output[ 'CSS', 'HTML' 3 min read How to Merge/Flatten an array of arrays in JavaScript ? Merging or flattening an array of arrays in JavaScript involves combining multiple nested arrays into a single-level array. This process involves iterating through each nested array and appending its elements to a new array, resulting in a flattened structure.To Merge or flatten array we can use arr 3 min read Types of Arrays in JavaScript A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi 3 min read JavaScript - Sort an Array of Strings Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana', 3 min read Javascript Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= .....Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR { 4 min read JavaScript- Arrays are Equal or Not These are the following approaches to compare two arrays in JavaScript:1. Using the JSON.stringify() MethodJavaScript provides a function JSON.stringify() method in order to convert an object whether or array into a JSON string. By converting it into JSON strings, we can directly check if the string 4 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 Find Mode of an Array using JavaScript To find the mode in an array with JavaScript, where the mode is the number occurring most frequently. Various approaches can be employed to find the mode, and an array may have multiple modes if multiple numbers occur with the same highest frequency. Example:Input:3, 6, 4, 6, 3, 6, 6, 7 , 6, 3 , 3Ou 4 min read JavaScript Array sort() Method The JS array.sort() method rearranges the array elements alphabetically and returns a sorted array. It does not create a new array but updates the original array.Sorting Array of StringsJavaScript// Original array let ar = ["JS", "HTML", "CSS"]; console.log(ar); // Sorting the array ar.sort() consol 4 min read JavaScript - Sort an Array in Reverse Order in JS JS array.sort() Method sorts the array in ascending order. We can make it descending or reverse order using array.reverse() method. Below are the following ways to sort an array in reverse order:1. Using array.sort() with array.reverse()First, sort the given array of strings using JS array.sort() me 2 min read Like