BMBM
BMBM

Reputation: 16013

locale aware string comparision

I´m using strcmp in combination with usort in order to sort an array of country names. Currently, the sort order is:

Belgien
Frankreich
Italien
Luxemburg
Niederlande
Spanien
United Kingdom
Österreich

Which is correct, apart from the position of Österreich. It should be between Niederlande and Spanien.

I also tried strnatcmp and strcoll (with setlocale), but the sort order was not the way I wanted it. The results are not from a mysql db, so sorting via a mysql query is not an option.

Upvotes: 2

Views: 1758

Answers (2)

BMBM
BMBM

Reputation: 16013

Old question, meanwhile I am working at another company on another project, but faced recently the same problem. What finally worked was installing the intl extension for PHP.

sudo apt-get install php5-intl

And then using:

$arr = array(
"Belgien",
"Frankreich",
"Italien",
"Luxemburg",
"Niederlande",
"United Kingdom",
"Österreich",
"Spanien",
"Ásdf",
);

$coll = collator_create('de_DE');
$coll->sort($arr);
print_r($arr);

Returned the results in the expected order:

Array
(
    [0] => Ásdf
    [1] => Belgien
    [2] => Frankreich
    [3] => Italien
    [4] => Luxemburg
    [5] => Niederlande
    [6] => Österreich
    [7] => Spanien
    [8] => United Kingdom
)

Upvotes: 5

Artefacto
Artefacto

Reputation: 97815

This works (assumes script is in UTF-8):

<?php

$arr = array(
"Belgien",
"Frankreich",
"Italien",
"Luxemburg",
"Niederlande",
"United Kingdom",
"Österreich",
"Spanien",
"Ásdf",
);

setlocale(LC_COLLATE, "pt_PT.UTF8");
usort($arr, 'strcoll');
print_r($arr);

gives me:

Array
(
    [0] => Ásdf
    [1] => Belgien
    [2] => Frankreich
    [3] => Italien
    [4] => Luxemburg
    [5] => Niederlande
    [6] => Österreich
    [7] => Spanien
    [8] => United Kingdom
)

However, this is painful; it requires the locale to be installed. locale -a gives you the installed locales, e.g. in my machine it gives me:

C
en_US
en_US.iso88591
en_US.utf8
POSIX
pt_PT.utf8

Upvotes: 0

Related Questions