note 80439 deleted from function.asort by vrana

From: Date: Fri, 13 Nov 2009 12:34:21 +0000
Subject: note 80439 deleted from function.asort by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162863@lists.php.net to get a copy of this message
Note Submitter: mzvarik at gmail dot com ---- I made a sorting function for czech alphabetics, maybe not the most effective function, but it did what I needed. <?php function myStrCmp($a, $b) { $cz_chars = "Aa Áá Bb Cc Čč Dd Ďď Ee Éé Ěě Ff Gg Hh Ii Íí Jj Kk Ll Mm Nn Ňň Oo Óó Pp Qq Rr Řř Ss Šš Tt Ťť Uu Úú Ůů Vv Ww Xx Yy Ýý Zz Žž"; $cz_ch = 39; // CH if($a == $b) return 0; $pos = 0; $advantage = false; while(true) { if(!isset($a{$pos})) { $result = !isset($b{$pos}) ? $advantage : -1; break; } elseif(!isset($b{$pos})) { $result = 1; break; } // A if(strtolower($a{$pos}) == 'c' && strtolower($a{$pos+1}) == 'h') { // CH $aa = $cz_ch; } else { $aa = strpos($cz_chars, $a{$pos}); } // B if(strtolower($b{$pos}) == 'c' && strtolower($b{$pos+1}) == 'h') { $bb = $cz_ch; } else { $bb = strpos($cz_chars, $b{$pos}); } if($aa === false) return 1; if($bb === false) return -1; if($aa != $bb) { $diff = $aa-$bb; if($diff > 1 || $diff < -1) { $result = $aa > $bb ? -1 : 1; break; } elseif(!$advantage) { $advantage = $diff == 1 ? -1 : 1; } } $pos++; } return $result; } function MySort(&$array, $orderby = 'asc') { $orderby = $orderby == 'asc' ? 1 : -1; $keys = array_keys($array); $values = array_values($array); $sorted[0] = array_shift($values); $count = count($values); foreach($values as $i => $wordA) { for($j=0; $j<=$i; $j++) { if(myStrCmp($wordA, $sorted[$j]) == $orderby) break; } array_splice($sorted, $j, 0, array($wordA)); } $array = array_combine($keys, $sorted); } ?>

« previous php.notes (#162863) next »