mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 12:17:58 +00:00
Add BoundedVec::sort_by_key (#11998)
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
committed by
GitHub
parent
a685582bfd
commit
85e2092d8f
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user