https://github.com/azu/v8-stable-sort-case-study
Stable sort case study.
https://github.com/azu/v8-stable-sort-case-study
nodejs v8
Last synced: about 1 month ago
JSON representation
Stable sort case study.
- Host: GitHub
- URL: https://github.com/azu/v8-stable-sort-case-study
- Owner: azu
- Created: 2018-12-26T07:14:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-26T07:30:49.000Z (over 6 years ago)
- Last Synced: 2024-10-05T12:42:39.570Z (7 months ago)
- Topics: nodejs, v8
- Language: JavaScript
- Size: 1.95 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Stable sort case study
Node.js 11(V8 7.0) use stable sort.
- [Node v11.0.0 (Current) | Node.js](https://nodejs.org/en/blog/release/v11.0.0/)
- [Getting things sorted in V8 · V8](https://v8.dev/blog/array-sort)It means that `Array#sort` cause different result in some case.
## Example
We want to arrange the array in descending order.
If you want to tests, do following commands.
- Get Node.js 10 result: `npm run node10`
- Get Node.js 11 result: `npm run node11````js
const array = [
{
id: "a",
index: 0
},
{
id: "b",
index: 1
},
{
id: "c",
index: 1
},
{
id: "d",
index: 3
},
{
id: "e",
index: 2
}
];const sortedArray = array.sort((a, b) => {
if (a.index <= b.index) {
return 1
} else {
return -1;
}
});
// Node.js@10: [ 'd', 'e', 'c', 'b', 'a' ]
// Node.js@11: [ 'd', 'e', 'b', 'c', 'a' ]
console.log(sortedArray.map(item => item.id));
```