DEV Community

Roman
Roman

Posted on

Reversing sort order in Rust

Rust allows sort items by key easily with .sort_by_key, here is example, it will put important notes to the end:

#[derive(Debug)]
struct Note {
    important: bool,
    title: &'static str,
}
let mut notes = vec![
    Note { important: false, title: "ddd"},
    Note { important: true, title: "aaa"},
    Note { important: false, title: "ccc"},
];
notes.sort_by_key(|k| k.important);
println!("{:?}", notes);
Enter fullscreen mode Exit fullscreen mode

But reversing order of sorting with key may be not so obvious, it is achieved with Reverse helper struct, here is how it can be applied to above example, in order to put important notes first:

use std::cmp::Reverse;
// ...
notes.sort_by_key(|k| Reverse(k.important));
println!("{:?}", notes);
Enter fullscreen mode Exit fullscreen mode

How is it implemented? Let's look at the source code of Rust standard library:

pub struct Reverse<T>(pub T);

impl<T: Ord> Ord for Reverse<T> {
    fn cmp(&self, other: &Reverse<T>) -> Ordering {
        other.0.cmp(&self.0)
    }
}
Enter fullscreen mode Exit fullscreen mode

The implementation is straightforward, it is newtype wrapper for original type, and for types with Ord trait it reimplements Ord but instead of comparing self with other it comparing other with self meaning it will reverse order in the result. Simple and beautiful!

And it will play nicely with complex keys too, lets put important notes first, and sort by title ascending for same importance groups:

notes.sort_by_key(|k| (Reverse(k.important), k.title));
println!("{:?}", notes);
Enter fullscreen mode Exit fullscreen mode

Of course in some cases it is possible to just negate value, like in this example using !k.important will produce same results, but negating is not always possible (eg. strings). Regarding performance I've benchmarked both methods and have not found any difference, both methods require same time if ordering booleans.

My links

I'm making my perfect todo, note-taking app Heaplist, you can try it here heaplist.app
I have a twitter @rsk
And a website rsk

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up