Sort An Array Of Arrays In JavaScript
Last Updated :
15 Nov, 2024
The following approaches can be used to sort array of arrays in JavaScript.
1. Using array.sort() Method- Mostly Used
JS 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 Column
To 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 Algorithm
Bubble 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 sorting
3. Using Selection Sort Algorithm
Selection 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 sorting
4. Using Insertion Sort Algorithm
Insertion 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
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