feat: sort_by を追加する - #77
Open
Javakky wants to merge 1 commit into
Open
Conversation
selector が返す比較キーの昇順で配列をソートする関数を追加する。 usort/uasort の安定ソート仕様をそのまま利用し、比較キーが同値の 要素は入力順を維持する(phpdoc に明記)。MODE_ASSOC はキー保持、 MODE_LIST は 0 始まりで再付番する。selector の呼び出しは要素ごと に 1 回、事前計算したキャッシュで比較する実装を選択した。 Closes #72
This was referenced Jul 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要(What / Why)
selector が返す比較キーで昇順ソートする
sort_byを追加します。親 issue #56 の breakdown で挙がっていた基本操作で、
usortのコンパレータを毎回書かずに済みます。変更点
src/sort_by.php—<=>で比較。ASSOC はキー保持、LIST は連番へ再付番tests/SortByTest.php— 10 ケースcomposer.jsonのautoload.filesに 1 件追加動作確認
sort_by(['carol' => 3, 'alice' => 1, 'bob' => 2], fn ($v, $k) => $k)→['alice' => 1, 'bob' => 2, 'carol' => 3]vendor/bin/php-cs-fixer fix --dry-run --diff/vendor/bin/phpstan analyse -c phpstan.neon/vendor/bin/phpunit testsOK (107 tests, 112 assertions)/ PHPStan level 10[OK] No errors/ CS FixerFixed 0 of 31 files補足(任意)
usort/uasortが安定なので、比較キーが同値の要素は入力順を維持します (phpdoc に明記、テストで担保)。issue feat: sort_by を追加する #72 の「安定性は仕様明記」要件への対応ですusortは参照渡しなので、{key, value, sort_key}のエントリ配列を組み立ててからソートし、入力を破壊しません (テストで担保)@phpstan-paramの条件型は付けていません。付けると「assoc 入力 +MODE_LIST」が型エラーになるためで、src/slice.php/src/map.php/src/intersect.phpと同じ形ですCloses #72
🤖 Generated with Claude Code