0

I want to sort a vector of structs, by comparing several fields of the structs. Example:

my_vec.sort_unstable_by_key(|k| (k.a, k.b, k.c));

That looks perfect. However, types like f32 and f64 only implement PartialOrd and not Ord (due to their special NaN values). So if some of those fields in the structs are f64 for example, I cannot use the method above.

I know I can get the job done in several ways, but I'd like to know what would be the simplest solution for cases like this?

at54321
  • 8,726
  • 26
  • 46
  • Using the [ordered_float](https://docs.rs/ordered-float) crate with it's [`OrderedFloat`](https://docs.rs/ordered-float/2.8.0/ordered_float/struct.OrderedFloat.html) newtype is probably the easiest way, albeit slightly verbose. – Filipe Rodrigues Nov 10 '21 at 12:36
  • Use `sort_by` instead and follow the advice in [this answer](https://stackoverflow.com/a/50308360) to construct the sorting criteria closure. – E_net4 Nov 10 '21 at 12:46

0 Answers0