Skip to content

feat(stdlib): sort by a key with sort_by_key and sorted_by_key 🔑 - #579

Merged
timfennis merged 1 commit into
masterfrom
feature/sort-by-key
Dec 1, 2026
Merged

timfennis merged 1 commit into
masterfrom
feature/sort-by-key

Conversation

@timfennis

Copy link
Copy Markdown
Owner

Sorting by part of each element, such as a pair's count, took a comparator: xs.sort_by(fn(a, b) => b[1] <=> a[1]). Now a key function is enough: xs.sort_by_key(fn(x) => -x[1]).

  • sort_by_key(list, key) sorts a list in place, and sorted_by_key(seq, key) gives a sorted list from any sequence sorted_by takes. The smallest key comes first.
  • Both are stable, and the key function runs once per element, not once per comparison.
  • As with sort_by, a key function that changes the list it's sorting is an error, not a silent reorder.
  • sort_by and sort_by_key share the code that sorts a copy and writes it back.
  • The manual's list page has a Sorting section covering sort, sorted and the key forms.

Written with AI (Claude Code), all of it.

🤖 Generated with Claude Code

Co-authored-by: Claude Opus 5.5 noreply@anthropic.com

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T06:02:39.262323Z 9454a4f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@timfennis
timfennis merged commit 66a767a into master Dec 1, 2026
1 check passed
@timfennis
timfennis deleted the feature/sort-by-key branch December 1, 2026 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant