sort_by
fn sort_by<T, K: Ord>(Vec<T> arr, T -> K key) -> Vec<T> Import
Source: use collections::{sort_by}; src/collections.hy Returns a copy of arr sorted by the value key gives each element, in ascending order.
Stable: elements with equal keys keep their order. key is called for each
comparison, so keep it cheap.
Parameters#
| Name | Type |
|---|---|
arr | Vec<T> |
key | T -> K |
Returns#
Vec<T>
Examples#
use collections::{sort_by};
use tests::assert;
let words: Vec<string> = ["kiwi", "fig", "banana"];
let by_len = sort_by(words, fn (string w) => w.len());
assert!(by_len[0] == "fig");
assert!(by_len[2] == "banana");
See also#
sort: Returns a sorted copy ofarr, in ascending order.reverse: Returns a copy ofarrin reverse order.collect_ints: Returns the values of the rangea..bas aVec<int>.collect_ints_inclusive: Returns the values of the inclusive rangea..=bas aVec<int>.