note 104009 added to function.sort

From: Date: Tue, 17 May 2011 10:10:05 +0000
Subject: note 104009 added to function.sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-179779@lists.php.net to get a copy of this message
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 // START Pre-sorting (Umlaut -> normal letters) $max = count($dat); for($totcnt = 0; $totcnt < $max; $totcnt++){ $dat[$totcnt]=str_replace('&szlig;','ss_',$dat[$totcnt]); $dat[$totcnt]=str_replace('&Auml;','Ae_',$dat[$totcnt]); $dat[$totcnt]=str_replace('&auml;','ae_',$dat[$totcnt]); $dat[$totcnt]=str_replace('&Ouml;','Oe_',$dat[$totcnt]); $dat[$totcnt]=str_replace('&ouml;','oe_',$dat[$totcnt]); $dat[$totcnt]=str_replace('&Uuml;','Ue_',$dat[$totcnt]); $dat[$totcnt]=str_replace('&uuml;','ue_',$dat[$totcnt]); } // END Pre-sorting (Umlaut -> normal letters) // START Sorting // function compare_towns($a, $b) { return strnatcmp($a, $b); } usort($dat, 'compare_towns'); // END Sorting // // START Post-sorting (normal letters -> Umlaut) for($totcnt = 0; $totcnt < $max; $totcnt++){ $dat[$totcnt]=str_replace('ss_','&szlig;',$dat[$totcnt]); $dat[$totcnt]=str_replace('Ae_','&Auml;',$dat[$totcnt]); $dat[$totcnt]=str_replace('ae_','&auml;',$dat[$totcnt]); $dat[$totcnt]=str_replace('Oe_','&Ouml;',$dat[$totcnt]); $dat[$totcnt]=str_replace('oe_','&ouml;',$dat[$totcnt]); $dat[$totcnt]=str_replace('Ue_','&Uuml;',$dat[$totcnt]); $dat[$totcnt]=str_replace('ue_','&uuml;',$dat[$totcnt]); } // END Post-sorting (normal letters -> Umlaut) ?> ---- Server IP: 69.147.83.197 Probable Submitter: 80.145.0.156 ---- Manual Page -- http://www.php.net/manual/en/function.sort.php Edit -- https://master.php.net/note/edit/104009 Del: integrated -- https://master.php.net/note/delete/104009/integrated Del: useless -- https://master.php.net/note/delete/104009/useless Del: bad code -- https://master.php.net/note/delete/104009/bad+code Del: spam -- https://master.php.net/note/delete/104009/spam Del: non-english -- https://master.php.net/note/delete/104009/non-english Del: in docs -- https://master.php.net/note/delete/104009/in+docs Del: other reasons-- https://master.php.net/note/delete/104009 Reject -- https://master.php.net/note/reject/104009 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179779) next »