Skip to content

Add compare argument for array.sorted - #5371

Closed
bluebear94 wants to merge 2 commits into
typst:mainfrom
bluebear94:mf/array-sorted-by
Closed

bluebear94 wants to merge 2 commits into
typst:mainfrom
bluebear94:mf/array-sorted-by

Conversation

@bluebear94

Copy link
Copy Markdown
Contributor

Fixes #5365.

I used compare for the argument name instead of by because by could plausibly refer to either the key or the comparison function.

According to the Rust standard library documentation, sorting might panic when a non-total ordering function is provided. I’ve been unable to produce a panic so far, but I think this is an important issue to resolve. We could catch_unwind such a panic, but this doesn’t solve the problem when panic=abort is turned on.

@laurmaedje

Copy link
Copy Markdown
Member

catch_unwind also doesn't work on wasm, so it's kind of a non-starter :/

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Dec 5, 2024
@laurmaedje

Copy link
Copy Markdown
Member

How shall we proceed here?

@bluebear94

Copy link
Copy Markdown
Contributor Author

Honestly, I’m not sure and that’s why it’s taken a long time for me to comment back. I think this addition isn’t worth the possible downsides, at least until we get more info on how limiting the current key system is. At the same time, as @MDLC01 mentioned, some comparison functions are hard to express as keys right now (such as sorting by A descending and then B ascending, or even sorting strings in descending order).

@MDLC01

MDLC01 commented Dec 12, 2024

Copy link
Copy Markdown
Collaborator

In cases where a comparison function is provided, maybe we could simply implement our own sorting algorithm? After all, the cost of calling the function probably outweighs the time it takes to sort the array, even if we do not do it with an algorithm as optimized as the one used by Rust.

@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label Dec 13, 2024
@laurmaedje

Copy link
Copy Markdown
Member

I guess we could do that.

@laurmaedje

Copy link
Copy Markdown
Member

@bluebear94 I'd be on board with having a simple custom sorting implementation for that case. If you want to implement that, feel free to. Otherwise, I think we can close the PR.

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.

Add compare argument to array.sorted, as an alternative to key

3 participants