Skip to main content

default_base_sort

Function default_base_sort 

Source
pub fn default_base_sort<T>(
    a: &RankedItem<'_, T>,
    b: &RankedItem<'_, T>,
) -> Ordering
Expand description

Alphabetical tiebreaker sort for ranked items.

Compares two ranked items by their ranked_value field using standard byte-wise string ordering (str::cmp). This is the Rust equivalent of the JS localeCompare used as the default baseSort in the original match-sorter library.

§Arguments

  • a - First ranked item
  • b - Second ranked item

§Returns

Ordering based on alphabetical comparison of ranked_value strings.

§Examples

use std::borrow::Cow;
use matchsorter::{RankedItem, Ranking, default_base_sort};
use std::cmp::Ordering;

let item_a = "apple".to_owned();
let item_b = "banana".to_owned();

let a = RankedItem {
    item: &item_a,
    index: 0,
    rank: Ranking::Equal,
    ranked_value: Cow::Borrowed("apple"),
    key_index: 0,
    key_threshold: None,
};
let b = RankedItem {
    item: &item_b,
    index: 1,
    rank: Ranking::Equal,
    ranked_value: Cow::Borrowed("banana"),
    key_index: 0,
    key_threshold: None,
};

assert_eq!(default_base_sort(&a, &b), Ordering::Less);