- sort
- sort_by
Sort vector of strings using sorting condition
examples/vectors/sort-strings/src/main.rs
fn main() { let mut animals = vec![ String::from("snake"), String::from("crab"), String::from("camel"), String::from("elephant"), String::from("lizard"), ]; println!("{animals:?}"); animals.sort(); println!("{animals:?}"); animals.sort_by(|a, b| a.len().cmp(&b.len())); println!("{animals:?}"); }
["snake", "crab", "camel", "elephant", "lizard"] ["camel", "crab", "elephant", "lizard", "snake"] ["crab", "camel", "snake", "lizard", "elephant"]