Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IE-0027: Sorting with custom comparison phrases #27

Merged
merged 1 commit into from
Jul 19, 2023
Merged

Conversation

curiousdannii
Copy link
Collaborator

(IE-0027) Sorting with custom comparison phrases

  • Proposal: IE-0027
  • Author: Dannii Willis
  • Language feature name: None
  • Status: In progress
  • Related proposals: None

Summary

Add phrases that allow lists and tables to be sorted with custom comparison
phrases.

@curiousdannii curiousdannii merged commit d12cbd2 into main Jul 19, 2023
@curiousdannii curiousdannii deleted the ie-0027 branch July 19, 2023 22:28
@curiousdannii curiousdannii added the formal-proposal A formal proposal that has been accepted for consideration by the core Inform team label Jul 20, 2023
@curiousdannii curiousdannii changed the title Add IE-0027: Sorting with custom comparison phrases IE-0027: Sorting with custom comparison phrases Jul 20, 2023
@CelticMinstrel
Copy link

CelticMinstrel commented Apr 14, 2024

The list phrase functions just as other common languages do (for example, see C#, Javascript). The phrase takes two list elements and returns a number indicating the relative order of the elements: negative if the left value should come before the right, zero if they are equal, and positive if the left value should come after the right.

I think it might be better to take the new C++ comparison system as a model and have the comparator functions return an enumerated type with either three or four possible values (depending on whether you want sort to work on a partial order). In particular, this cuts off the common but incorrect practice of returning the difference of two numbers as the comparison result. (It's incorrect because of overflow – so it can be correct, but only with an unlimited precision bignum type.)

Since there is no kind representing an entire table row, to sort a table we pass the table name and two row numbers. The comparison phrase can then access any columns it wishes in order to determine the relative order of the two rows. Any completely blank rows will be put at the bottom, just as with the existing sorting phrases.

SQL offers an option to put blank rows (or rather, nulls) at the top instead, is there any benefit to that? Maybe there isn't, but I just thought it was worth asking. If desired it could just be a phrase option ("blanks first or blanks last). (This question actually applies to the pre-existing sorts as well.)

Side note while I'm here: the documentation doesn't state whether sorts of lists are stable or not. This seems like a pretty important thing to know. It does mention that sorting a table is stable, but I think you should be explicit about it in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formal-proposal A formal proposal that has been accepted for consideration by the core Inform team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants