Open In App

Is JavaScript Array Sort Stable ?

Last Updated : 17 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads