Skip to content

Commit

Permalink
Fix array list on sort description
Browse files Browse the repository at this point in the history
  • Loading branch information
kmkzt authored and mfuji09 committed Jul 5, 2023
1 parent 60d3072 commit 49e06bf
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ const compareFn = (a, b) => a > b ? 1 : 0;
arr.sort(compareFn);
```

ここでの `compareFn` 関数は、対称性を満たしていないため、正しい形式ではありません。 `a > b` ならば `1` を返しますが、 `a``b` を入れ替えると、負の値ではなく `0` を返すようになります。そのため、生成される配列はエンジンによって異なります。例えば、V8(Chrome、Node.jsなどで使用)やJavaScriptCore(Safariで使用)は配列を全くソートせず、 `[3, 1, 4, 1, 5, 9]` を返しますが、SpiderMonkey(Firefoxで使用)は `[1, 1, 4, 5, 9]` のように昇順に並べた配列を返すことになります。
ここでの `compareFn` 関数は、対称性を満たしていないため、正しい形式ではありません。 `a > b` ならば `1` を返しますが、 `a``b` を入れ替えると、負の値ではなく `0` を返すようになります。そのため、生成される配列はエンジンによって異なります。例えば、V8(Chrome、Node.jsなどで使用)やJavaScriptCore(Safariで使用)は配列を全くソートせず、 `[3, 1, 4, 1, 5, 9]` を返しますが、SpiderMonkey(Firefoxで使用)は `[1, 1, 3, 4, 5, 9]` のように昇順に並べた配列を返すことになります。

しかし、`compareFn` 関数を少し変更して、`-1``0` を返すようにすると、次のようになります。

Expand Down

0 comments on commit 49e06bf

Please sign in to comment.