Wednesday, July 3, 2024
HomeLanguagesJavascriptIs JavaScript Array Sort Stable ?

Is JavaScript Array Sort Stable ?

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments