Function in collections coil-stdlib

sort_by

fn sort_by<T, K: Ord>(Vec<T> arr, T -> K key) -> Vec<T>
Import
use collections::{sort_by};
Source: 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#

NameType
arrVec<T>
keyT -> 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 of arr, in ascending order.
  • reverse: Returns a copy of arr in reverse order.
  • collect_ints: Returns the values of the range a..b as a Vec<int>.
  • collect_ints_inclusive: Returns the values of the inclusive range a..=b as a Vec<int>.
↑ ↓ navigate ↵ open