ES2019 introduced a revision to the Array sort() method.
Prior to 2019, the specification permitted unstable sorting algorithms like QuickSort.
Post-ES2019, browsers are required to implement a stable sorting algorithm:
When sorting elements based on a value, those elements must maintain their relative positions with respect to other elements possessing the same value.
Example
const myArr = [ {name:“X00”,price:100 }, {name:“X01”,price:100 }, {name:“X02”,price:100 }, {name:“X03”,price:100 }, {name:“X04”,price:110 }, {name:“X05”,price:110 }, {name:“X06”,price:110 }, {name:“X07”,price:110 } ]; |
In the example provided, when sorting based on price, it’s imperative that the resulting order does not deviate in such a way that the names end up in different relative positions.
X01 100 X03 100 X00 100 X03 100 X05 110 X04 110 X06 110 X07 110 |