Search documentation of Localize

# Localize.Collation.Numeric (Localize v0.39.0)

Copy Markdown View Source

Numeric collation support (kn=true / numeric=true).

When enabled, sequences of decimal digits are treated as numeric values for primary sorting, ensuring "file2" sorts before "file10".

The numeric value is encoded as a length-prefixed big-endian number in the primary weight.

## Summary

### Functions

encode_numeric_value(codepoints)

Encode a sequence of digit codepoints as numeric collation elements.

process_elements(ce_pairs)

Process codepoint/element pairs, replacing digit sequence CEs with numeric-value-based CEs.

## Functions

# encode_numeric_value(codepoints)

Encode a sequence of digit codepoints as numeric collation elements.

### Arguments

- `codepoints` - a list of integer codepoints representing decimal digits.

### Returns

A list of elements: one length-prefix CE followed by one CE per significant digit.

### Examples

    iex> result = Localize.Collation.Numeric.encode_numeric_value([0x31, 0x30])
    iex> length(result)
    3

# process_elements(ce_pairs)

    @spec process_elements([{[non_neg_integer()], [Localize.Collation.Element.t()]}]) :: [
      Localize.Collation.Element.t()
    ]

Process codepoint/element pairs, replacing digit sequence CEs with numeric-value-based CEs.

### Arguments

- `ce_pairs` - a list of `{codepoints, [element]}` pairs.

### Returns

A flat list of element structs with digit sequences replaced by numeric collation elements.

### Examples

    iex> pairs = [{[0x31], [{0x21E7, 0x0020, 0x0002, false}]}, {[0x30], [{0x21E6, 0x0020, 0x0002, false}]}]
    iex> result = Localize.Collation.Numeric.process_elements(pairs)
    iex> length(result)
    3