Is JavaScript Array Sort Stable ?
Last Updated :
17 Feb, 2023
Javascript Array Sort is stable: As of version 10 (or ECMAScript 2019), the specification requires Array.prototype.sort to be stable. In javascript, stable sorting is if an array objects with (key, value) pair has equal keys appear in the same order in sorted output as they appear in the input data set. Therefore it is reliable for the sorting of key-value pairs.
The JavaScript Array sort() method is used to sort an array in a specified order according to the compare() function. If the method is takeout, the array is sorted in ascending order.
Before version 10 (or ECMA 2019), sort stability was not guaranteed i.e. sometimes it shows stability and sometimes not. Therefore, it gives a wrong output for the given input.
Syntax:
Array.sort()
Example 1: In this example, we have shown how the sorting was before version 10.
JavaScript
const students = [
{ name: "Kavya", Age: 21 },
{ name: "Mansi", Age: 22 },
{ name: "Riya", Age: 20 },
{ name: "Sanya", Age: 20 },
];
// Compare function
students.sort((first, second) => (first.Age - second.Age));
console.log(students);
Output:
[
{ name: 'Sanya', Age: 20 }, / Original order not maintained
{ name: 'Riya', Age: 20 }, // Original order not maintained
{ name: 'Kavya', Age: 21 },
{ name: 'Mansi', Age: 22 }
]
This example changes its order because stability is not reliable in an earlier version. Therefore, it is not stable.
Example 2: In this example, the sort() method shows stability in sorting.
JavaScript
const students = [
{ name: "Kavya", Age: 21 },
{ name: "Mansi", Age: 22 },
{ name: "Riya", Age: 20 },
{ name: "Sanya", Age: 20 },
];
// Compare function
students.sort((first, second) => (first.Age - second.Age));
console.log(students);
Output:
[
{ name: 'Riya', Age: 20 },
{ name: 'Sanya', Age: 20 },
{ name: 'Kavya', Age: 21 },
{ name: 'Mansi', Age: 22 }
]
The above example is already sorted by name but when we sort the age by using the compare function then the output does not change the order of name ('Riya' and 'Sanya'). This shows the stability in sorting.
Example 3: In this example, it shows stability in sorting.
JavaScript
const fruits = [
{ name: "Apple", cost: 100 },
{ name: "Banana", cost: 60 },
{ name: "Pineapple", cost: 200 },
{ name: "Pears", cost: 200 },
{ name: "Pomegranate", cost: 200 },
{ name: "Strawberry", cost: 150 },
];
fruits.sort((a, b) => a.cost - b.cost);
console.log(fruits);
Output:
[
{ name: 'Banana', cost: 60 },
{ name: 'Apple', cost: 100 },
{ name: 'Strawberry', cost: 150 },
{ name: 'Pineapple', cost: 200 }, // Original order maintained
{ name: 'Pears', cost: 200 }, // Original order maintained
{ name: 'Pomegranate', cost: 200 } // Original order maintained
]
In the above example, the name is already sorted alphabetically but when we sort by using comparing function the order of names does not change which means sorting in javascript is stable.
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 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
JavaScript- Merge two Sorted Arrays These are the following ways to merge two sorted arrays: 1. Using Two Pointers (Efficient For Large Arrays)This approach involves using two pointers, one for each array, and comparing their elements as you iterate through them. This method works efficiently in O(n + m) time, where n and m are the le
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 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 Array toSorted() Method The JS arr.toSorted() method rearranges the array elements alphabetically and returns a new sorted array.Sorting Array of StringsJavaScriptconst arr = ["JS", "HTML", "CSS"]; const arr1 = arr.toSorted(); console.log(arr); console.log(arr1);Output[ 'JS', 'HTML', 'CSS' ][ 'CSS', 'HTML', 'JS' ]Array.toS
3 min read
Sort An Array Of Arrays In JavaScript 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.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [
2 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 - 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
Merge K Sorted Array using JavaScript We are given K sorted arrays and our task is to merge them into a single sorted array in JavaScript.Example:Input: arrays = [[2, 4, 5], [1, 3, 8], [0, 7, 9]];Output: [0, 1, 2, 3, 4, 5, 7, 8]These are the following approaches:Table of ContentNaive ApproachDivide and Conquer ApproachMin-Heap ApproachN
5 min read