Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Use a stable sort algorithm for Iterable.sortedBy #192

Merged
merged 2 commits into from
May 6, 2021

Conversation

xvrh
Copy link
Contributor

@xvrh xvrh commented May 2, 2021

The random nature of the quick sort algorithm is surprising for an extension like sortedBy.

For reference, C# uses a stable sort for Enumerable.OrderBy

@google-cla google-cla bot added the cla: yes label May 2, 2021
@natebosch natebosch requested a review from lrhn May 5, 2021 18:43
@lrhn
Copy link
Contributor

lrhn commented May 5, 2021

I don't know why people keep expecting stable sorts, but apparently the do.
Looks fine to me.

Do update the version number in pubspec.yaml to the next minor version number and add a CHANGELOG.md entry explaining the change.

@tatumizer
Copy link

tatumizer commented May 6, 2021

I don't know why people keep expecting stable sorts, but apparently they do.

Example: suppose you are an innovator composer and you are writing a suite for 2 sirens. Each siren has a distinct tone controlled by 2 commands: "siren on" and "siren off". Using a computer program written in dart, you generate 2 tracks independently, each track being sorted in ascending order of timestamps. The intervals between "siren on" and "siren off" can sometimes be zero due to rounding, but "siren on" always comes first. When you mix 2 tracks together, you want to preserve the original order of timestamps, otherwise you can end up in a situation where "siren off" comes before "siren on", which will result in an unusually long note that may disturb the neighbors.

(Example taken from my own practice)

@xvrh
Copy link
Contributor Author

xvrh commented May 6, 2021

Thanks @lrhn

Do update the version number in pubspec.yaml to the next minor version number and add a CHANGELOG.md entry explaining the change.

Done.

@lrhn
Copy link
Contributor

lrhn commented May 6, 2021

LGTM. Thanks!

@tatumizer That's a perfectly good example of why people who assume stable sorting will get bitten. It's why you'd assume that to begin with that surprises me (but I guess I've just implemented enough quicksorts to make be immune to that assumption).

Quicksort is faster and more space efficient, so I'm a little sad to see something slower being the default.

@lrhn lrhn merged commit 3c4db06 into dart-archive:master May 6, 2021
@tatumizer
Copy link

I think every beginner programmer gets bitten by this at least once. It's not that easy to realize what you assume - every misunderstanding comes from some wrong implicit assumption. But WRT sorting, the (implicit) stability assumption comes from a common-sense argument: when we sort something by hand, we never swap equal things; the computer must be smarter than me, hence it won't swap them either.
(Dont' take my comment too seriously: I'm long past the stage of composing for sirens - moved on to steam whistles already :-)).

mosuem pushed a commit to dart-lang/core that referenced this pull request Oct 18, 2024
…ction#192)

* Use a stable sort algorithm for Iterable.sortedBy

* Increment version number and add changelog entry
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants