-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Added core::cmp::Reverse for sort_by_key reverse sorting #40720
Conversation
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
This seems like a nice ergonomic win to me. Thoughts @rust-lang/libs? |
It would be possible to relax the constraints a bit, so that |
Stability attributes should be fixed. |
How about a free function |
@malbarbo what would the function do? Create a "Reverse" object? |
@mitsuhiko Yes. This is similar to free functions in |
I was going with |
Alternative name proposals:
|
Seems like a nice addition to me! |
I personally like |
Good to see it handling I don't like Maybe Or maybe something involving |
src/libcore/cmp.rs
Outdated
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]); | ||
/// ``` | ||
#[derive(PartialEq, Eq, Debug)] | ||
#[stable(feature = "rust1", since = "1.18.0")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to start out unstable.
In my own project I called it |
We discussed this PR in the libs triage meeting today, and everyone was in favor of the addition. We're also fine with both the name and API. (It's a slight shift from other APIs in Other than changing the annotations to |
@mitsuhiko Ah sorry, to clarify the unstable attribute will need to point to a fresh "tracking issue" like #40494 (make sure to tag B-unstable). You can go ahead and create that. |
@aturon i created the issue but lack the rights to flag it appropriately. So it's untagged for now. |
@bors r+ |
📌 Commit 5d36953 has been approved by |
Thanks @mitsuhiko! :-) |
Wohoo \o/ Thanks :) |
…ushi Added core::cmp::Reverse for sort_by_key reverse sorting I'm not sure if this is the best way to go about proposing this feature but it's pretty useful. It allows you to use `sort_by_key` and return tuples where a single item is then reversed to how it normally sorts. I quite miss something like this in Rust currently though I'm not sure if this is the best way to implement it.
Implement all PartialOrd methods for Reverse When making a forwarding wrapper we must in general forward all methods, so that we use the type's own `lt` for example instead of the default. Example important case: f32's partial_cmp does several operations but its lt is a primitive. Follow up on rust-lang#40720
/// v.sort_by_key(|&num| (num > 3, Reverse(num))); | ||
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]); | ||
/// ``` | ||
#[derive(PartialEq, Eq, Debug)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we derive PartialEq and Eq here, when we are specifying custom implementations below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no custom implementation for PartialEq
, only for Ord
and PartialOrd
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, of course. I don't know where my brain was. Sorry about that.
I'm not sure if this is the best way to go about proposing this feature but it's pretty useful. It allows you to use
sort_by_key
and return tuples where a single item is then reversed to how it normally sorts.I quite miss something like this in Rust currently though I'm not sure if this is the best way to implement it.