I read up on various problems re: sort() and German Umlaut chars and my head was soon spinning - bug in sort() or not, solution via locale or not, etc. ... (a total newbie here).
The obvious solution for me was quick and dirty: transform the Umlaut chars (present as HTML codes in my case) to their normal equivalent ('ä' = 'ae', 'ö' = 'oe', 'ü' = 'ue', 'ß' = 'ss' etc.), sort the array, then transform back. However there are cases in which a 'Mueller' is really that and does NOT need to be transformed into 'Müller' afterwards. Hence I for example replace the Umlaut itself with it's normal equivalent plus a char not used in the string otherwise (e.g. '_') so that the transfer back to Umlaut would only take place on certain combinations.
Of course any other char instead of '_' can be used as additional char (influencing the sort result). I know that my solution is rough at the edges and may cause other sort problems but it was sufficient for my purpose.
The array '$dat' in this example was filled with German town names (I actually worked with a multiple array ('$dat[][]') but stripped the code down to this as it's easier to understand):
<?php
$max = count($dat);
for($totcnt = 0; $totcnt < $max; $totcnt++){
$dat[$totcnt]=str_replace('ß','ss_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Ä','Ae_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('ä','ae_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Ö','Oe_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('ö','oe_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Ü','Ue_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('ü','ue_',$dat[$totcnt]);
}
function compare_towns($a, $b)
{
return strnatcmp($a, $b);
}
usort($dat, 'compare_towns');
for($totcnt = 0; $totcnt < $max; $totcnt++){
$dat[$totcnt]=str_replace('ss_','ß',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Ae_','Ä',$dat[$totcnt]);
$dat[$totcnt]=str_replace('ae_','ä',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Oe_','Ö',$dat[$totcnt]);
$dat[$totcnt]=str_replace('oe_','ö',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Ue_','Ü',$dat[$totcnt]);
$dat[$totcnt]=str_replace('ue_','ü',$dat[$totcnt]);
}
?>