Class: ICU4X::Collator

Inherits:
Object
  • Object
show all
Defined in:
lib/icu4x/yard_docs.rb

Overview

Compares strings according to locale-specific collation rules.

Collator provides locale-aware string comparison for sorting and searching.

Examples:

Basic comparison

collator = ICU4X::Collator.new(locale)
collator.compare("a", "b")  #=> -1
collator.compare("b", "a")  #=> 1
collator.compare("a", "a")  #=> 0

Sorting with collator

collator = ICU4X::Collator.new(locale)
words.sort { |a, b| collator.compare(a, b) }

Numeric sorting

collator = ICU4X::Collator.new(locale, numeric: true)
collator.compare("file2", "file10")  #=> -1  (2 < 10)

Instance Method Summary collapse

Constructor Details

#initialize(locale, provider: nil, sensitivity: :variant, numeric: false, case_first: nil) ⇒ Collator

Creates a new Collator instance.

Parameters:

  • locale (Locale)

    the locale for collation rules

  • provider (DataProvider, nil) (defaults to: nil)

    data provider (uses default if nil)

  • sensitivity (Symbol) (defaults to: :variant)

    comparison sensitivity: ‘:base`, `:accent`, `:case`, or `:variant`

  • numeric (Boolean) (defaults to: false)

    whether to compare numeric strings as numbers

  • case_first (Symbol, nil) (defaults to: nil)

    which case to sort first: ‘:upper` or `:lower`

Raises:

  • (DataError)

    if data for the locale is unavailable



816
817
# File 'lib/icu4x/yard_docs.rb', line 816

def initialize(locale, provider: nil, sensitivity: :variant,
numeric: false, case_first: nil); end

Instance Method Details

#compare(a, b) ⇒ Integer

Compares two strings.

Parameters:

  • a (String)

    first string

  • b (String)

    second string

Returns:

  • (Integer)

    -1 if a < b, 0 if a == b, 1 if a > b



825
# File 'lib/icu4x/yard_docs.rb', line 825

def compare(a, b); end

#resolved_optionsHash

Returns the resolved options for this instance.

Returns:

  • (Hash)

    options hash with keys:

    • ‘:locale` [String] the resolved locale identifier

    • ‘:sensitivity` [Symbol] the comparison sensitivity

    • ‘:numeric` [Boolean] whether numeric sorting is enabled

    • ‘:case_first` [Symbol] which case sorts first (if set)



835
# File 'lib/icu4x/yard_docs.rb', line 835

def resolved_options; end