How to constrain a generic argument with Index trait

Welcome to the strange world of float comparisons. You need to unwrap the result of partial_cmp, because comparing floats is not that simple, i.e. NaN != NaN.

data.sort_by(|a,b| b[idx].partial_cmp(&a[idx]).unwrap());

See e.g. this topic:

@H2CO3 recently shared a helpful link with more information about float comparisons, but unfortunately I don't recall which topic it was, nor do I remember the webpage.

1 Like