Function rudac::algo::find::kth_with [−][src]
pub fn kth_with<T, F>(slice: &mut [T], k: usize, compare: &F) -> usize where
F: Fn(&T, &T) -> Ordering,
Expand description
Returns index of kth smallest item in the slice using a customized closure for comparison
Arguments
slice
: slice of unordered datak
: kthcompare
: custom comparing closure
Panics
- panics if k is out of range: 0 <= k < slice.len()
Examples
use rudac::algo::find::kth_with;
// consider a vector of 2d points
let mut vec = vec![(1, 10), (2,6), (3,1), (3,4), (4,2), (5,3), (6,7), (8,9), (9,8), (10,5)];
// find index of kth smallest point based on their y axis only
let fifth_item_index = kth_with(&mut vec, 4, &|x1,x2| {x1.1.cmp(&x2.1)});
assert_eq!(vec[fifth_item_index], (10,5));