DEV Community

How to sort a vector of a custom struct in Rust

Thiago Massari Guedes on March 08, 2024

How to sort a vector of a custom struct in Rust When implementing tags for Texted2, I had a list of tags being: let tags: Vec<(&am...
Collapse
 
thiagomg profile image
Thiago Massari Guedes

That's a nice simpler alternative, indeed :)
I find it less readable, however.

Collapse
 
lexlohr profile image
Alex Lohr

Why is that? Also, if you are concerned it could be misunderstood, put the lambda in a separate named function called reverse_order or something.

Collapse
 
thiagomg profile image
Thiago Massari Guedes

Now this is on the preference realm. After living many, many years in the low latency world, where things are not super readable, I like explicit things when possible, such as the block:

Ordering::Less => Ordering::Greater,
Ordering::Greater => Ordering::Less,
Enter fullscreen mode Exit fullscreen mode

And, of course, that's my personal preference :) Both are ok - and I agree with you that the function name is better using a descriptive name.

Thread Thread
 
thiagomg profile image
Thiago Massari Guedes

And thanks for the feedback!

Collapse
 
lexlohr profile image
Alex Lohr

Wouldn't it be simpler to count_b.cmp(count_a) to change the ordering?