[][src]Function radsort::sort_by_key

pub fn sort_by_key<T, F, K>(slice: &mut [T], key_fn: F) where
    F: FnMut(&T) -> K,
    K: Key

Sorts the slice using a key extraction function.

Key can be any scalar type. See Key for a full list.

This sort is stable (i.e., does not reorder equal elements) and O(w m n), where the key function is O(m) and w is the size of the key in bytes.

Allocates temporary storage the size of the slice.

See sort_by_cached_key if you use expensive key function or if you need to sort large elements.

Panics

Can panic if the key function returns different keys for the same element when called repeatedly. The panic is on a best-effort basis. In case of panic, the order of elements in the slice is not specified.

Examples

let mut friends = ["Punchy", "Isabelle", "Sly", "Puddles", "Gladys"];
 
// sort by the length of the string in bytes
radsort::sort_by_key(&mut friends, |s| s.len());
 
assert_eq!(friends, ["Sly", "Punchy", "Gladys", "Puddles", "Isabelle"]);