Skip to content

unstable_sort_by vs panics #136665

Closed
Closed
@mitsuhiko

Description

@mitsuhiko

Location

slice::sort_unstable_by

Summary

I ran into a bug with total sort order again which caused a panic. While looking for what others were doing in that situation I ran into a pull request where someone attempted to work around this with unstable sort (cantino/mcfly#454).

Reading the documentation implies in the first part that the worst that happens is not a sorted slice, whereas in the end it mentions that it can panic:

If the comparison function compare does not implement a total order the resulting order of elements in the slice is unspecified.

later

May panic if compare does not implement a total order.

The documentation on sort_by is clearer in that regard:

If the comparison function compare does not implement a total order, the function may panic; even if the function exits normally, the resulting order of elements in the slice is unspecified. See also the note on panicking below.

This should probably be aligned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions