Skip to content

Instantly share code, notes, and snippets.

@BillBarnhill
Created June 23, 2023 16:02
Show Gist options
  • Save BillBarnhill/e3bf2b0b21f96831b714564642481984 to your computer and use it in GitHub Desktop.
Save BillBarnhill/e3bf2b0b21f96831b714564642481984 to your computer and use it in GitHub Desktop.
Rust: Sort a vector of ints and preserve indices
use std::cmp::Ordering;
fn main() {
fn sort_pairs<'r,'s>((_,a): &'r (usize, &i32), (_,b): &'s (usize, &i32)) -> Ordering {
if a > b { Ordering::Greater }
else if b > a { Ordering::Less }
else { Ordering::Equal}
}
let mut v: Vec<(usize,&i32)> = [1, 3, 2].iter().enumerate().collect();
v.sort_by(sort_pairs);
println!("{:?}", v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment