PrePAN

Sign in to PrePAN

Sort::Naturally::ICU Perl extension for human-friendly ("natural") sort order, which using ICU library for locale aware sorting.

Good

Synopsis

use Sort::Naturally::ICU qw/nsort ncmp sorted/;

my @mixed_list = qw/test21 test20 test10 test11 test2 test1/;

my @result = nsort(@mixed_list);
# @result is: test1 test2 test10 test11 test20 test21

@result = sort ncmp @mixed_list;
# same, but use standard sort function

my $list = ['a'..'c', 'A'..'C'];

my $result_us = sorted($list, locale => 'en-US-u-va-posix');
# $result_us contains A, B, C, a, b, c

my $result_ca = sorted($list, locale => 'en-CA-u-va-posix');
# $result_ca contains a, A, b, B, c, C

Description

DESCRIPTION

See: http://search.cpan.org/~serval/Sort-Naturally-XS-0.7.8/lib/Sort/Naturally/XS.pm#DESCRIPTION

LOCALE AWARE SORTING

The following example demonstrates default way to locale aware sorting:

use POSIX;

my @list = ('a'..'c', 'A'..'C');

setlocale(POSIX::LC_ALL, 'en_US.utf8');
my @result = sort @list;
# @result contains  a, A, b, B, c, C

setlocale(POSIX::LC_ALL, 'en_CA.utf8');
@result = sort @list;
# @result contains  A, a, B, b, C, c

The problem is that not all Unix-like OSs completely support POSIX, in fact only Linux fully POSIX compatible. Therefore you can't use above approach in Mac OS or FreeBSD. This module is designed to solve this issue.

To be able to sort a list with an arbitrary locale at any platform it's necessary to use the sorted function with a locale keyword argument. locale should be LDML locale identifier:

use Sort::Naturally::ICU qw/sorted/;

my $list = ['a'..'c', 'A'..'C'];

my $result_us = sorted($list, locale => 'en-US-u-va-posix');
# $result_us contains A, B, C, a, b, c

my $result_ca = sorted($list, locale => 'en-CA-u-va-posix');
# $result_ca contains a, A, b, B, c, C

Comments

Please sign up to post a review.