use num_traits::{Float, ToPrimitive};
use crate::vector::Vector;
pub trait SortOps<T> {
fn sorted(&self) -> Vector<T>;
fn sort_in_place(&mut self);
}
impl<T> SortOps<T> for Vector<T>
where
T: Float + ToPrimitive + Copy + PartialOrd,
{
fn sorted(&self) -> Vector<T> {
let mut sorted = self.clone();
sorted.sort_in_place();
sorted
}
fn sort_in_place(&mut self) {
self.0.sort_by(|a, b| {
match (a.partial_cmp(b), a.is_nan(), b.is_nan()) {
(Some(ordering), _, _) => ordering,
(None, false, true) => std::cmp::Ordering::Less, (None, true, false) => std::cmp::Ordering::Greater, _ => std::cmp::Ordering::Equal, }
});
}
}