Created
June 23, 2023 16:02
-
-
Save BillBarnhill/e3bf2b0b21f96831b714564642481984 to your computer and use it in GitHub Desktop.
Rust: Sort a vector of ints and preserve indices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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