Skip to content

Commit

Permalink
Merge pull request #657 from salano-ym/arr-stable-sort
Browse files Browse the repository at this point in the history
Next: arr.sortを安定ソートにする
  • Loading branch information
salano-ym authored May 16, 2024
2 parents 732d63a + ed279f1 commit 112d1f7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- **Breaking Change** 関数同士の比較の実装
- **Breaking Change** `+``!`などの演算子の優先順位に変更があります。新しい順序は[syntax.md](docs/syntax.md#%E6%BC%94%E7%AE%97%E5%AD%90)を参照して下さい。
- **Breaking Change** 組み込み関数`Num:to_hex`は組み込みプロパティ`num#to_hex`に移動しました。
- **Breaking Change** `arr.sort`を安定ソートに変更

# 未リリース分

Expand Down
1 change: 1 addition & 0 deletions docs/primitive-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ _fromIndex_が負値の時は末尾からの位置(配列の長さ+_fromIndex_
### @(_v_: arr).sort(_comp_: @(_a_: value, _b_: value)): arr
**【この操作は配列を書き換えます】**
配列の並べ替えをします。第1引数 _comp_ として次のような比較関数を渡します。
安定ソートです。
* _a__b_ より順番的に前の時、負の値を返す
* _a__b_ より順番的に後の時、正の値を返す
* _a__b_ と順番的に同等の時、0を返す
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/primitive-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ const PRIMITIVE_PROPS: {
const r = right[rightIndex]!;
const compValue = await opts.call(comp, [l, r]);
assertNumber(compValue);
if (compValue.value < 0) {
if (compValue.value <= 0) {
result.push(left[leftIndex]!);
leftIndex++;
} else {
Expand Down
17 changes: 17 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3214,6 +3214,23 @@ describe('primitive props', () => {
`);
eq(res, ARR([OBJ(new Map([['x', NUM(2)]])), OBJ(new Map([['x', NUM(3)]])), OBJ(new Map([['x', NUM(10)]]))]));
});

test.concurrent('sort (stable)', async () => {
const res = await exe(`
var arr = [[2, 0], [10, 1], [3, 2], [3, 3], [2, 4]]
let comp = @(a, b) { a[0] - b[0] }
arr.sort(comp)
<: arr
`);
eq(res, ARR([
ARR([NUM(2), NUM(0)]),
ARR([NUM(2), NUM(4)]),
ARR([NUM(3), NUM(2)]),
ARR([NUM(3), NUM(3)]),
ARR([NUM(10), NUM(1)]),
]));
});

test.concurrent('fill', async () => {
const res = await exe(`
Expand Down

0 comments on commit 112d1f7

Please sign in to comment.