diff --git a/src/moves.rs b/src/moves.rs index c4e35bb..272e078 100644 --- a/src/moves.rs +++ b/src/moves.rs @@ -298,11 +298,32 @@ impl<'l> Moves<'l> { }) } - /// Sorts the moves in the list. - /// - /// See [`slice::sort_unstable_by`] for potential panics. + /// Sorts the list in ascending order with a comparison function, preserving initial order of + /// equal elements. See `std::slice::sort_by` for details. #[inline] pub fn sort_by(&mut self, mut compare: F) + where + F: FnMut(Move, Move) -> core::cmp::Ordering, + { + self.array.as_mut_slice().sort_by(|a, b| { + compare( + Move { + position: self.position, + raw: *a, + }, + Move { + position: self.position, + raw: *b, + }, + ) + }); + } + + /// Sorts the list in ascending order with a comparison function, **without** preserving the + /// initial order of equal elements. See [`sort_unstable_by`](`slice::sort_unstable_by`) for + /// details. + #[inline] + pub fn sort_unstable_by(&mut self, mut compare: F) where F: FnMut(Move, Move) -> core::cmp::Ordering, {