note 85620 added to function.sort

From: Date: Tue, 09 Sep 2008 14:43:15 +0000
Subject: note 85620 added to function.sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-144262@lists.php.net to get a copy of this message
To sort an array of multiple text fields alphabetically you have to make the text lowercase before sorting the array. Otherwise PHP puts acronyms before words. You can see this in my example code. Simply store the original text field at the end of the array line and call it later from there. You can safely ignore the lowercase version which is added to the start of the array line. <?php echo '<pre>ORIGINAL DATA: <br />'; $data = array( 'Saturn|7|8|9|0||', 'Hello|0|1|2|3||', 'SFX|5|3|2|4||', 'HP|9|0|5|6||' ); print_r($data); sort($data); reset($data); echo '<br />RAW SORT: <br />'; print_r($data); for ($c = 0; $c < count($data); $c++) { list ($letter,$g1,$g2,$g3,$g4,$end) = explode ('|', $data[$c]); $lowercase = strtolower($letter); $data2[$c] = array($lowercase,$g1,$g2,$g3,$g4,$letter); } sort($data2); reset($data2); echo '<br />LOWERCASE SORT: <br />'; print_r($data2); echo '</pre>'; ?> ---- Server IP: 92.48.74.199 Probable Submitter: 143.53.227.75 ---- Manual Page -- http://www.php.net/manual/en/function.sort.php Edit -- https://master.php.net/note/edit/85620 Del: integrated -- https://master.php.net/note/delete/85620/integrated Del: useless -- https://master.php.net/note/delete/85620/useless Del: bad code -- https://master.php.net/note/delete/85620/bad+code Del: spam -- https://master.php.net/note/delete/85620/spam Del: non-english -- https://master.php.net/note/delete/85620/non-english Del: in docs -- https://master.php.net/note/delete/85620/in+docs Del: other reasons-- https://master.php.net/note/delete/85620 Reject -- https://master.php.net/note/reject/85620 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144262) next »