Skip to content

Friendly hint and docs clarification for array.sorted - #7528

Merged
laurmaedje merged 2 commits into
typst:mainfrom
cady-b:sort-hint
Dec 5, 2025
Merged

laurmaedje merged 2 commits into
typst:mainfrom
cady-b:sort-hint

Conversation

@cady-b

@cady-b cady-b commented Dec 4, 2025 •

Copy link
Copy Markdown
Contributor

Background

0.14.1 (#7520) changed the algorithm used by array.sorted; from slice::sort_by (driftsort) to glidesort. This slightly alters which elements are actually compared1, which causes inputs that previously passed silently despite not technically having been valid to now error more frequently (but still not always).

Previous discussion on Discord: Typst › contributors 🗪

Samples

input 0.14.0 0.14.1
((1, []), (1, [])) ❌ ❌
((1, []), (2, [])) ✅ ✅
range(1,8).map(v => (v, [])) ✅ ✅
range(1,9).map(v => (v, [])) ✅ ❌
((1, 1), (2, 1), (3, 1), (4, []), (5, 1), (6, 1), (7, 1), (8, 1)) ✅ ✅
((1, 1), (2, 1), (3, 1), (4, 1), (5, []), (6, 1), (7, 1), (8, 1)) ✅ ❌

New Hint

Especially the volatility makes debugging this issue in one's document difficult. To counteract the source of confusion, I propose a new hint be added to the error generated in this code path:

error: cannot compare content and content
  ┌─ a.typ:1:1
  │
1 │ #range(1,9).map(v => (v, [])).sorted()
  │  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  │
  = hint: consider choosing a `key` or defining the comparison with `by`

It's not too noisy if the user knows something about the input array was wrong (see the changes for existing tests), but helps those determined to sort the given input find their way to a solution. We present keywords which help experienced users recall their options and allow newer ones to look up the most relevant information in the documentation.

The hint isn't applied to the branch where by is given because at that point we can't do much; the user has to figure out how to solve the error case. Same for an error within key.

Personally, I really don't like that invalid inputs sometimes work, but also see that checking this more extensively would be pretty difficult. Maybe something to consider for the future alongside #6285 (comment).

Doc Comment change

Not much to say here. If you believe it doesn't matter as much as I feel rn but like the hint, I'll gladly drop this part.

Footnotes

  1. https://github.com/Voultapher/sort-research-rs/blob/main/writeup/driftsort_introduction/text.md#components-from-glidesort-which-are-not-incorporated ↩

cady-b and others added 2 commits December 4, 2025 18:13
The recent switch to glidesort made it more common to run into this code
path, i.e. with `range(1,9).map(v => (v, [])).sorted()`. Let's be nice
and point users in the right direction c:
@laurmaedje
laurmaedje enabled auto-merge December 5, 2025 15:50
@laurmaedje

Copy link
Copy Markdown
Member

Thanks! This is nice. :)

@laurmaedje
laurmaedje added this pull request to the merge queue Dec 5, 2025
Merged via the queue into typst:main with commit f3d08bf Dec 5, 2025
@cady-b
cady-b deleted the sort-hint branch December 5, 2025 16:59
laurmaedje added a commit that referenced this pull request Dec 11, 2025
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
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.

2 participants