Closed
Description
Hi—
Ideally, the implementation of sort_by_key() would invoke the received key function exactly once per item. This is highly beneficial when a costly computation (e.g., a distance function) needs to be used for sorting.
But Rust’s implementation as of 1.9 (link) calls the key function each time:
pub fn sort_by_key(&mut self, mut f: F)
{
self.sort_by(|a, b| f(a).cmp(&f(b)))
}
For comparison, on its day Python highlighted this in the release notes for 2.4. As per their current Sorting HOW TO:
The value of the key parameter should be a function that takes a single argument and returns a key to use for sorting purposes. This technique is fast because the key function is called exactly once for each input record.
Many thanks for considering. It’d be great if Rust could behave the same way.