note 44798 deleted from function.usort by felipe
| From: | felipe@php.net | Date: | Thu, 03 Jan 2008 02:16:14 +0000 |
| Subject: | note 44798 deleted from function.usort by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134707@lists.php.net to get a copy of this message | ||
Note Submitter: gk at lka dot hu
----
We have written some functions to sort arrays by national ABC's (or by other custom
letter-orders).
function sort_hun($array)
{
usort($array,"huncmp");
return($array);
}
function intcmp($a,$b,$ALP)
{
if ($a==$b) return 0;
$ALPL = strlen($ALP);
$ap = $bp = -1;
$i = 0;
while (($i < $ALPL) and (($ap == -1) or ($bp == -1)))
{
if ($ALP[$i] == $a) $ap = $i;
if ($ALP[$i] == $b) $bp = $i;
$i++;
}
return($ap < $bp) ? -1 :1;
}
function huncmp($astring, $bstring)
{
$ALP = "AaÁáBbCcDdEeÉéFfGgHhIiÍiJjKkLlMmNnOoÓóÖöÕõPpQqRr".
"SsTtUuÚúÜüÛûVvWwXxYyZz0123456789!?.()[]=%+-";
//if equal
if ($astring == $bstring) return 0;
//do it on every element
for ($i = 0; $i<strlen($astring) && $i<strlen($bstring) &&
$astring[$i]==$bstring[$i]; $i++);
//if the two strings are the same, the sorter wins
if ($astring[$i]==$bstring[$i]) return (strlen($astring) > $bstring) ? -1 :1;
//otherwise depends on the first different char
return(intcmp($astring[$i],$bstring[$i],$ALP));
}
-----
How to call it? Here is a sample:
$ize =
array("jani","béla","ágnes","ÁGNES","zazie","érdekes");
$ize = sort_hun($ize);
(It returns "Array ( [0] => ÁGNES [1] => ágnes [2] => béla [3] => érdekes [4]
=> jani [5] => zazie )".)