Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an array of UTF-8 strings?

I currentyl have no clue on how to sort an array which contains UTF-8 encoded strings in PHP. The array comes from a LDAP server so sorting via a database (would be no problem) is no solution. The following does not work on my windows development machine (although I'd think that this should be at least a possible solution):

$array=array('Birnen', 'Äpfel', 'Ungetüme', 'Apfel', 'Ungetiere', 'Österreich'); $oldLocal=setlocale(LC_COLLATE, "0"); var_dump(setlocale(LC_COLLATE, 'German_Germany.65001')); usort($array, 'strcoll'); var_dump(setlocale(LC_COLLATE, $oldLocal)); var_dump($array); 

The output is:

string(20) "German_Germany.65001" string(1) "C" array(6) {   [0]=>   string(6) "Birnen"   [1]=>   string(9) "Ungetiere"   [2]=>   string(6) "Äpfel"   [3]=>   string(5) "Apfel"   [4]=>   string(9) "Ungetüme"   [5]=>   string(11) "Österreich" } 

This is complete nonsense. Using 1252 as the codepage for setlocale() gives another output but still a plainly wrong one:

string(19) "German_Germany.1252" string(1) "C" array(6) {   [0]=>   string(11) "Österreich"   [1]=>   string(6) "Äpfel"   [2]=>   string(5) "Apfel"   [3]=>   string(6) "Birnen"   [4]=>   string(9) "Ungetüme"   [5]=>   string(9) "Ungetiere" } 

Is there a way to sort an array with UTF-8 strings locale aware?

Just noted that this seems to be PHP on Windows problem, as the same snippet with de_DE.utf8 used as locale works on a Linux machine. Nevertheless a solution for this Windows-specific problem would be nice...

like image 830
Stefan Gehrig Avatar asked Sep 23 '08 11:09

Stefan Gehrig


People also ask

How do you sort an array of strings?

To sort a String array in Java, you need to compare each element of the array to all the remaining elements, if the result is greater than 0, swap them.

Can you use sort () on an array of strings?

To sort an array of strings in Java, we can use Arrays. sort() function.

How to sort an array of strings js?

In JavaScript arrays have a sort( ) method that sorts the array items into an alphabetical order. The sort( ) method accepts an optional argument which is a function that compares two elements of the array. If the compare function is omitted, then the sort( ) method will sort the element based on the elements values.

How do you sort an array of strings in descending order?

To sort an array in Java in descending order, you have to use the reverseOrder() method from the Collections class. The reverseOrder() method does not parse the array. Instead, it will merely reverse the natural ordering of the array.


1 Answers

$a = array( 'Кръстев', 'Делян1', 'делян1', 'Делян2', 'делян3', 'кръстев' ); $col = new \Collator('bg_BG'); $col->asort( $a ); var_dump( $a ); 

Prints:

array   2 => string 'делян1' (length=11)   1 => string 'Делян1' (length=11)   3 => string 'Делян2' (length=11)   4 => string 'делян3' (length=11)   5 => string 'кръстев' (length=14)   0 => string 'Кръстев' (length=14) 

The Collator class is defined in PECL intl extension. It is distributed with PHP 5.3 sources but might be disabled for some builds. E.g. in Debian it is in package php5-intl .

Collator::compare is useful for usort.

like image 127
Delian Krustev Avatar answered Oct 01 '22 13:10

Delian Krustev