note 44798 added to function.usort

From: Date: Tue, 17 Aug 2004 00:28:13 +0000
Subject: note 44798 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-74784@lists.php.net to get a copy of this message
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 )".) ---- Manual Page -- http://www.php.net/manual/en/function.usort.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44798 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44798&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44798&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#74784) next »