I was looking at the documentation and found an example code that looked unfamiliar.
use std::cmp::Reverse;
let mut v = vec![1, 2, 3, 4, 5, 6];
v.sort_by_key(|&num| (num > 3, Reverse(num)));
assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
How does (num > 3, Reverse(num))
define ordering between themselves?
I had a look into documentation for tuple, and it said
The sequential nature of the tuple applies to its implementations of various traits. For example, in PartialOrd and Ord, the elements are compared sequentially until the first non-equal set is found.
That makes sense for equality checks, but it seems to me that it does not give explanation for how >
and <
acts on tuples.
I did some experiments, but understood nothing.
println!("{}", (5, 5) > (3, 4)); // true
println!("{}", (2, 2) > (3, 4)); // false
println!("{}", (2, 5) > (3, 4)); // false
println!("{}", (3, 5) > (3, 4)); // true
println!("{}", (5, 2) > (3, 4)); // true