Function feruca::collate

source · []
pub fn collate<T: AsRef<[u8]> + Eq + Ord>(
    a: &T,
    b: &T,
    opt: CollationOptions
) -> Ordering
Expand description

This is the main public function in the library. It accepts as arguments two string references or byte slices, and a CollationOptions struct. It returns an Ordering value. This is designed to be used in conjunction with the sort_by function in the standard library. Simple usage might look like the following…

use feruca::{collate, CollationOptions};

let mut names = ["Peng", "Peña", "Ernie", "Émile"];
names.sort_by(|a, b| collate(a, b, CollationOptions::default()));

let expected = ["Émile", "Ernie", "Peña", "Peng"];
assert_eq!(names, expected);

Significantly, in the event that two strings are ordered equally per the Unicode Collation Algorithm, this function will use byte-value comparison (i.e., the traditional, naïve way of sorting strings) as a tiebreaker. While this is probably appropriate in most cases, it can be avoided by using the collate_no_tiebreak function.