1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
use crate::total_ord::Total; use num::traits::Float; pub trait TotalSort { fn total_sort(&mut self); } impl<T> TotalSort for Vec<T> where T: PartialOrd + Float, { fn total_sort(&mut self) { let mut tmp: Vec<Total<T>> = self.iter().map(|&i| Total(i)).collect(); tmp.sort(); *self = tmp.iter().map(|&Total(i)| i).collect(); } } #[cfg(test)] mod test { use super::*; #[test] fn test_total_sort() { let mut test = vec![2.2f64, 1.0f64, 8.5f64, 0.5f64, -3.5f64]; let sorted: Vec<f64> = vec![-3.5f64, 0.5f64, 1.0f64, 2.2f64, 8.5f64]; test.total_sort(); assert_eq!(sorted, test); } }