Locale-sensitive string comparison, modelled on
Intl.Collator.
Compares and sorts strings according to the Unicode Collation Algorithm with CLDR locale tailoring.
Delegates to Localize.Collation for the underlying comparison.
Note: the JS Intl.Collator returns -1, 0, or 1 from compare.
This module returns :lt, :eq, or :gt following Elixir
conventions, making it compatible with Enum.sort/2.
Summary
Functions
Compares two strings according to locale collation rules.
Sorts a list of strings according to locale collation rules.
Functions
Compares two strings according to locale collation rules.
Arguments
string1is the first string.string2is the second string.optionsis a keyword list of options.
Options
:localeis a locale identifier string or atom. The default is the current process locale.:sensitivityis:base,:accent,:case, or:variant. The default is:variant.:case_firstis:upper,:lower, or:false. Determines whether upper or lower case sorts first.:numericis a boolean. Whentrue, numeric substrings are compared as numbers (so "2" < "10"). The default isfalse.:ignore_punctuationis a boolean. Whentrue, punctuation is ignored during comparison. The default isfalse.
Returns
:ltifstring1sorts beforestring2.:eqif the strings are equal under the collation rules.:gtifstring1sorts afterstring2.
Examples
iex> Intl.Collator.compare("a", "b", locale: :en)
:lt
iex> Intl.Collator.compare("b", "a", locale: :en)
:gt
iex> Intl.Collator.compare("a", "a", locale: :en)
:eq
Sorts a list of strings according to locale collation rules.
This is an Elixir convenience function not present in the JS Intl.Collator API.
Arguments
listis a list of strings.optionsis a keyword list of options. Accepts the same options ascompare/3.
Returns
- A sorted list of strings.
Examples
iex> Intl.Collator.sort(["banana", "apple", "cherry"], locale: :en)
["apple", "banana", "cherry"]