Compare structs for ordering (sorting) - Ord
-
Ord
-
PartialOrd
-
In order to be able to decide which object is "bigger" or "smaller" than the other one we need the
Ord
trait that requiresPartialOrd
trait and theEq
andPartialEq
traits. -
This will allow use to
sort
the values. -
Comaring the fields happen in the order the appear in the definition of ths struct. In our case Rust forst compares the 'number' fields. The 'name' fields are only compared if the 'number' fields are equal.
#[derive(PartialEq, Eq, PartialOrd, Ord)] struct Thing { number: i32, name: String, } fn main() { let a = Thing { number: 42, name: String::from("Foo"), }; let b = Thing { number: 43, name: String::from("Foo"), }; let c = Thing { number: 42, name: String::from("Bar"), }; let d = Thing { number: 43, name: String::from("Bar"), }; println!("{}", a < b); // becasue 42 < 43 println!("{}", c < b); // becasue 42 < 43 ( Bar < Foo is not even checked here) println!("{}", c < a); // because Bar < Foo println!("{}", c < d); // because 42 < 43 println!("{}", a < d); // because 42 < 43 // the comparisions is in the order of declaraion of the fields // So the `name` field is not checked here }
true
true
true
true
true