note 80439 added to function.asort

From: Date: Wed, 16 Jan 2008 10:46:40 +0000
Subject: note 80439 added to function.asort
Groups: php.notes 
Request: Send a blank email to php-notes+get-135132@lists.php.net to get a copy of this message
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); } ?> ---- Server IP: 212.24.129.51 Probable Submitter: 90.176.148.82 ---- Manual Page -- http://www.php.net/manual/en/function.asort.php Edit -- https://master.php.net/note/edit/80439 Del: integrated -- https://master.php.net/note/delete/80439/integrated Del: useless -- https://master.php.net/note/delete/80439/useless Del: bad code -- https://master.php.net/note/delete/80439/bad+code Del: spam -- https://master.php.net/note/delete/80439/spam Del: non-english -- https://master.php.net/note/delete/80439/non-english Del: in docs -- https://master.php.net/note/delete/80439/in+docs Del: other reasons-- https://master.php.net/note/delete/80439 Reject -- https://master.php.net/note/reject/80439 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135132) next »