4 releases
0.1.3 | May 6, 2023 |
---|---|
0.1.2 | Jul 26, 2020 |
0.1.1 | Jun 18, 2020 |
0.1.0 | Jun 17, 2020 |
#1983 in Algorithms
557 downloads per month
10KB
203 lines
Sort By
Convenience functions that allow for sorting iterators.
Example
use sortby::*;
#[derive(Clone, Debug, Eq, PartialEq)]
struct Person {
pub age: i32,
pub name: &'static str,
}
fn main() {
let data = vec![
Person {
name: "Rich",
age: 18,
},
Person {
name: "Bob",
age: 9,
},
Person {
name: "Marc",
age: 21,
},
Person {
name: "Alice",
age: 18,
},
];
let sorted: Vec<_> = data.iter()
.sort_by_desc(|p| p.age)
.then_sort_by(|p| p.name)
.collect();
println!("{:#?}", sorted);
}
lib.rs
:
This crate adds convenient sort functions for Iterators.
Example
use sortby::*;
#[derive(Clone, Debug, Eq, PartialEq)]
struct Person {
pub age: i32,
pub name: &'static str,
}
let data = vec![
Person {
name: "Rich",
age: 18,
},
Person {
name: "Bob",
age: 9,
},
Person {
name: "Marc",
age: 21,
},
Person {
name: "Alice",
age: 18,
},
];
let sorted: Vec<_> = data.iter()
.sort_by_desc(|p| p.age)
.then_sort_by(|p| p.name)
.collect();
println!("{:#?}", sorted);