Skip to content
Snippets Groups Projects
Commit 85e2092d authored by Oliver Tale-Yazdi's avatar Oliver Tale-Yazdi Committed by GitHub
Browse files

Add BoundedVec::sort_by_key (#11998)


Signed-off-by: default avatarOliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
parent a685582b
No related merge requests found
......@@ -319,6 +319,17 @@ impl<T, S> BoundedVec<T, S> {
self.0.sort_by(compare)
}
/// Exactly the same semantics as [`slice::sort_by_key`].
///
/// This is safe since sorting cannot change the number of elements in the vector.
pub fn sort_by_key<K, F>(&mut self, f: F)
where
F: FnMut(&T) -> K,
K: sp_std::cmp::Ord,
{
self.0.sort_by_key(f)
}
/// Exactly the same semantics as [`slice::sort`].
///
/// This is safe since sorting cannot change the number of elements in the vector.
......@@ -1189,4 +1200,12 @@ pub mod test {
b1.iter().map(|x| x + 1).rev().take(2).try_collect();
assert!(b2.is_err());
}
#[test]
fn bounded_vec_sort_by_key_works() {
let mut v: BoundedVec<i32, ConstU32<5>> = bounded_vec![-5, 4, 1, -3, 2];
// Sort by absolute value.
v.sort_by_key(|k| k.abs());
assert_eq!(v, vec![1, 2, -3, 4, -5]);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment