4 ms·
I like how Rust does it with the `Ordering` type and associated combinators: https://doc.rust-lang.org/stable/std/cmp/enum.Ordering.html https://doc.rust-lang.o
by romac 4y ago
I like how Rust does it with the `Ordering` type and associated combinators: https://doc.rust-lang.org/stable/std/cmp/enum.Ordering.html https://doc.rust-lang.org/stable/std/cmp/enum.Ordering.html
For two values `x` and `y` of the same type with fields `a` and `b`, this lets you compare on `a` first and then on `b` by writing
```
x.a.cmp(&y.a).then(x.b.cmp(&y.b))
```
- masklinn 4y agoIn fairness that was directly inspired by Haskell’s type of the same name: https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelude.html#t:Ordering https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelu...
- im3w1l 4y agoNot bad but I think python's way - using tuples is cleaner (x.a, x.b) < (y.a, y.b) btw you can format as code by indenting two spaces.