RS.CLIPPY.UNNECESSARY_SORT_BY
Use of `Vec::sort_by` when `Vec::sort_by_key` or `Vec::sort` would be clearer
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: unnecessary_sort_by. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for usage of Vec::sort_by passing in a closure
which compares the two arguments, either directly or indirectly.
Why is this bad?
It is more clear to use Vec::sort_by_key (or Vec::sort if
possible) than to use Vec::sort_by and a more complicated
closure.
Known problems
If the suggested Vec::sort_by_key uses Reverse and it isn't already
imported by a use statement, then it will need to be added manually.
Example
vec.sort_by(|a, b| a.foo().cmp(&b.foo()));
Use instead:
vec.sort_by_key(|a| a.foo());