note 54392 added to function.sort

From: Date: Sat, 02 Jul 2005 20:12:10 +0000
Subject: note 54392 added to function.sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-91319@lists.php.net to get a copy of this message
The usort solution below still has problems when sorting strings of different lengths, just like the other flavors of sort that I've tried. Here's a solution that I came up with -- it's kinda ugly, and it might not be practical for large arrays, but it's works great for me. I'm getting some people's names out of mySQL and placing them in an array like this: "$lastname, $firstname $middlename". Because some people don't have middle names, "sort" screws up the alphabetization. So what I've done is this: $index = 0; while ($rows = mysql_fetch_array($sqlResult)) { extract($rows); $my_array[$index]['sortname'] = strtolower(preg_replace("/[^a-zA-Z]/", "", "$lastname$firstname$middlename")); $my_array[$index]['name'] = trim("$lastname, $firstname $middlename"); $index++; } sort($my_array); // now in perfect alpha order My "hack" is in the fourth line -- I take the entire name, last name first, and use preg_replace() to take out all non-letter characters, and then used strtolower() to make it all lowercase to ensure all letters would be treated equally by PHP. This goes into the ['sortname'] key within the array. (The ['sortname'] value has to come before ['name'], or else the array will be sorted by ['name'] instead.) This gives me: Array ( [0] => Array ( [sortname] => shmojoe [name] => Shmo, Joe ) [1] => Array ( [sortname] => shmojoem [name] => Shmo, Joe M ) ) ... and so on So the array gets sorted by ['sortname'], which never needs to be displayed, and the associated ['name'] can be displayed in perfect alphabetical order, whether there is one name, two names or three. And because ['sortname'] is letters only, it works great for hyphenated last names or ones with apostrophes, like Smith-Jones or O'Donnell. I feel that the dual preg_replace() and strtolower() may not be the best theoretical solution, and I don't know how it would run on a really large array, but as I said, it's a great solution for my little site. ---- Manual Page -- http://www.php.net/manual/en/function.sort.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54392 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54392&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54392&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54392&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54392&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54392&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54392&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54392&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91319) next »