Uh oh!
There was an error while loading. Please reload this page.
Added core::cmp::Reverse for sort_by_key reverse sorting - #40720
Conversation
rust-highfive
commented
Mar 21, 2017
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
sfackler
commented
Mar 22, 2017
This seems like a nice ergonomic win to me. Thoughts @rust-lang/libs? |
ranma42
commented
Mar 22, 2017
It would be possible to relax the constraints a bit, so that |
nagisa
commented
Mar 22, 2017
Stability attributes should be fixed. |
malbarbo
commented
Mar 22, 2017
How about a free function |
mitsuhiko
commented
Mar 22, 2017
@malbarbo what would the function do? Create a "Reverse" object? |
malbarbo
commented
Mar 22, 2017
@mitsuhiko Yes. This is similar to free functions in |
mitsuhiko
commented
Mar 22, 2017
I was going with |
mitsuhiko
commented
Mar 22, 2017
Alternative name proposals:
|
alexcrichton
commented
Mar 27, 2017
Seems like a nice addition to me! |
alexcrichton
commented
Mar 27, 2017
I personally like |
scottmcm
commented
Mar 27, 2017
Good to see it handling I don't like Maybe Or maybe something involving |
| /// 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.
This will need to start out unstable.
mitsuhiko
commented
Mar 28, 2017
In my own project I called it |
aturon
commented
Mar 28, 2017
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 |
aturon
commented
Mar 28, 2017
@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. |
mitsuhiko
commented
Mar 29, 2017
@aturon i created the issue but lack the rights to flag it appropriately. So it's untagged for now. |
BurntSushi
commented
Mar 29, 2017
@bors r+ |
bors
commented
Mar 29, 2017
📌 Commit 5d36953 has been approved by |
BurntSushi
commented
Mar 29, 2017
Thanks @mitsuhiko! :-) |
mitsuhiko
commented
Mar 29, 2017
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.
Why do we derive PartialEq and Eq here, when we are specifying custom implementations below?
There was a problem hiding this comment.
There is no custom implementation for PartialEq, only for Ord and PartialOrd.
There was a problem hiding this comment.
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_keyand 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.