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 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 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 JavaScript - Sort a Numeric Array Here are some common methods to Sort Numeric Array using JavaScript. Please note that the sort function by default considers number arrays same as an array of stings and sort by considering digits as characters.Using sort() with a Comparison Function - Most UsedThe sort() method in JavaScript, when 2 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 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 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 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 Like