std-method
slice::sort_unstable_by_key
Sort in place by an extracted key. Equal keys are not guaranteed to retain their original order. Expected stdout (not executed): [(1, "book"), (2, "bag"), (3, "pen")] Common mistake: Depending on the initial order of equal-key records.
Formal Syntax & Structure
rust syntaxrust
slice.sort_unstable_by_key(|item| key)Working Example
Example Snippetrust
fn main() { let mut items = [(3, "pen"), (1, "book"), (2, "bag")]; items.sort_unstable_by_key(|item| item.0); println!("{items:?}");}