Help-Site Computer Manuals
  Algorithms & Data Structures   Programming Languages   Revision Control
  Cameras   Computers   Displays   Keyboards & Mice   Motherboards   Networking   Printers & Scanners   Storage
  Windows   Linux & Unix   Mac

use UCA

Unicode::Collate - use UCA


Unicode::Collate - use UCA (Unicode Collation Algorithm)


  use Unicode::Collate;


  $Collator = Unicode::Collate->new(%tailoring);


  @sorted = $Collator->sort(@not_sorted);


  $result = $Collator->cmp($a, $b); # returns 1, 0, or -1.


Constructor and Tailoring

The new method returns a collator object.

   $Collator = Unicode::Collate->new(

      alternate => $alternate,

      backwards => $levelNumber, # or \@levelNumbers

      entry => $element,

      normalization  => $normalization_form,

      ignoreName => qr/$ignoreName/,

      ignoreChar => qr/$ignoreChar/,

      katakana_before_hiragana => $bool,

      level => $collationLevel,

      overrideCJK => \&overrideCJK,

      overrideHangul => \&overrideHangul,

      preprocess => \&preprocess,

      rearrange => \@charList,

      table => $filename,

      undefName => qr/$undefName/,

      undefChar => qr/$undefChar/,

      upper_before_lower => $bool,


   # if %tailoring is false (empty),

   # $Collator should do the default collation.
-- see 3.2.2 Alternate Weighting, UTR #10.

   alternate => 'shifted', 'blanked', 'non-ignorable', or 'shift-trimmed'.

By default (if specification is omitted), 'shifted' is adopted.

-- see 3.1.2 French Accents, UTR #10.

     backwards => $levelNumber or \@levelNumbers

Weights in reverse order; ex. level 2 (diacritic ordering) in French. If omitted, forwards at all the levels.

-- see 3.1 Linguistic Features; 3.2.1 File Format, UTR #10.

Overrides a default order or adds a new collation element

  entry => <<'ENTRIES', # use the UCA file format

00E6 ; [.0861.0020.0002.00E6] [.08B1.0020.0002.00E6] # ligature <ae> as <a e>

0063 0068 ; [.0893.0020.0002.0063]      # "ch" in traditional Spanish

0043 0068 ; [.0893.0020.0008.0043]      # "Ch" in traditional Spanish


-- see Completely Ignorable, 3.2.2 Alternate Weighting, UTR #10.

Ignores the entry in the table. If an ignored collation element appears in the string to be collated, it is ignored as if the element had been deleted from there.

E.g. when 'a' and 'e' are ignored, 'element' is equal to 'lament' (or 'lmnt').

-- see 4.3 Form a sort key for each string, UTR #10.

Set the maximum level. Any higher levels than the specified one are ignored.

  Level 1: alphabetic ordering

  Level 2: diacritic ordering

  Level 3: case ordering

  Level 4: tie-breaking (e.g. in the case when alternate is 'shifted')

  ex.level => 2,

-- see 4.1 Normalize each input string, UTR #10.

If specified, strings are normalized before preparation of sort keys (the normalization is executed after preprocess).

As a form name, one of the following names must be used.

  'C'  or 'NFC'  for Normalization Form C

  'D'  or 'NFD'  for Normalization Form D

  'KC' or 'NFKC' for Normalization Form KC

  'KD' or 'NFKD' for Normalization Form KD

If omitted, the string is put into Normalization Form D.

If undefined explicitly (as normalization => undef), any normalization is not carried out (this may make tailoring easier if any normalization is not desired).


-- see 7.1 Derived Collation Elements, UTR #10.

By default, mapping of CJK Unified Ideographs uses the Unicode codepoint order and Hangul Syllables are decomposed into Hangul Jamo.

The mapping of CJK Unified Ideographs or Hangul Syllables may be overrided.

ex. CJK Unified Ideographs in the JIS codepoint order.

  overrideCJK => sub {

    my $u = shift;               # get unicode codepoint

    my $b = pack('n', $u);       # to UTF-16BE

    my $s = your_unicode_to_sjis_converter($b); # convert

    my $n = unpack('n', $s);     # convert sjis to short

    [ $n, 1, 1 ];                # return collation element


If you want to override the mapping of Hangul Syllables, the Normalization Forms D and KD are not appropriate (they will be decomposed before overriding).

-- see 5.1 Preprocessing, UTR #10.

If specified, the coderef is used to preprocess before the formation of sort keys.

ex. dropping English articles, such as ``a'' or ``the''. Then, ``the pen'' is before ``a pencil''.

     preprocess => sub {

           my $str = shift;

           $str =~ s/\b(?:an?|the)\s+//g;



-- see 3.1.3 Rearrangement, UTR #10.

Characters that are not coded in logical order and to be rearranged. By default,

    rearrange => [ 0x0E40..0x0E44, 0x0EC0..0x0EC4 ],

-- see 3.2 Default Unicode Collation Element Table, UTR #10.

You can use another element table if desired. The table file must be in your lib/Unicode/Collate directory.

By default, the file lib/Unicode/Collate/allkeys.txt is used.

If undefined explicitly (as table => undef), no file is read (you'd define collation elements using entry).

-- see 6.3.4 Reducing the Repertoire, UTR #10.

Undefines the collation element as if it were unassigned in the table. This reduces the size of the table. If an unassigned character appears in the string to be collated, the sort key is made from its codepoint as a single-character collation element, as it is greater than any other assigned collation elements (in the codepoint order among the unassigned characters). But, it'd be better to ignore characters unfamiliar to you and maybe never used.

-- see 6.6 Case Comparisons; 7.3.1 Tertiary Weight Table, UTR #10.

By default, lowercase is before uppercase and hiragana is before katakana.

If the parameter is true, this is reversed.

Other methods

@sorted = $Collator->sort(@not_sorted)
Sorts a list of strings.

$result = $Collator->cmp($a, $b)
Returns 1 (when $a is greater than $b) or 0 (when $a is equal to $b) or -1 (when $a is lesser than $b).

$result = $Collator->eq($a, $b)
$result = $Collator->ne($a, $b)
$result = $Collator->lt($a, $b)
$result = $Collator->le($a, $b)
$result = $Collator->gt($a, $b)
$result = $Collator->ge($a, $b)
They works like the same name operators as theirs.

   eq : whether $a is equal to $b.

   ne : whether $a is not equal to $b.

   lt : whether $a is lesser than $b.

   le : whether $a is lesser than $b or equal to $b.

   gt : whether $a is greater than $b.

   ge : whether $a is greater than $b or equal to $b.

$sortKey = $Collator->getSortKey($string)
-- see 4.3 Form a sort key for each string, UTR #10.

Returns a sort key.

You compare the sort keys using a binary comparison and get the result of the comparison of the strings using UCA.

   $Collator->getSortKey($a) cmp $Collator->getSortKey($b)

      is equivalent to

   $Collator->cmp($a, $b)

$position = $Collator->index($string, $substring)
($position, $length) = $Collator->index($string, $substring)
-- see 6.8 Searching, UTR #10.

If $substring matches a part of $string, returns the position of the first occurrence of the matching part in scalar context; in list context, returns a two-element list of the position and the length of the matching part.

Notice that the length of the matching part may differ from the length of $substring.

Note that the position and the length are counted on the string after the process of preprocess, normalization, and rearrangement. Therefore, in case the specified string is not binary equal to the preprocessed/normalized/rearranged string, the position and the length may differ form those on the specified string. But it is guaranteed that, if matched, it returns a non-negative value as $position.

If $substring does not match any part of $string, returns -1 in scalar context and an empty list in list context.

e.g. you say

  my $Collator = Unicode::Collate->new( normalization => undef, level => 1 );

  my $str = "Ich mu\x{00DF} studieren.";

  my $sub = "m\x{00FC}ss";

  my $match;

  if(my($pos,$len) = $Collator->index($str, $sub)){

      $match = substr($str, $pos, $len);


and get "mu\x{00DF}" in $match since "muß" is primary equal to "müss".


None by default.


Unicode::Collate has not been ported to EBCDIC. The code mostly would work just fine but a decision needs to be made: how the module should work in EBCDIC? Should the low 256 characters be understood as Unicode or as EBCDIC code points? Should one be chosen or should there be a way to do either? Or should such translation be left outside the module for the user to do, for example by using Encode::from_to()?


Use of the normalization parameter requires the Unicode::Normalize module.

If you need not it (say, in the case when you need not handle any combining characters), assign normalization => undef explicitly.

-- see 6.5 Avoiding Normalization, UTR #10.


SADAHIRO Tomoyuki, <>

  Copyright(C) 2001, SADAHIRO Tomoyuki. Japan. All rights reserved.

  This program is free software; you can redistribute it and/or 

  modify it under the same terms as Perl itself.


Unicode Collation Algorithm - Unicode TR #10

the Unicode::Normalize manpage
normalized forms of Unicode text