Re: [PHP] array_multisort into Natural order?
On 13 December 2010 19:59, George Langley george.lang...@shaw.ca wrote: Hi all. Can use natsort($array1) to sort a single array of filenames into a natural alphanumeric order - 1.php, 2.php, 5.php, 10.php, 20.php, etc. But using array_multisort($array1, $array2, $array3) doesn't offer a natsort option, so I end up with 1.php, 10.php, 2.php, 20.php, 5.php, etc. Anyone have a solution to this limitation already? Nothing found in the archives or Googling. Thanks. George Langley Multimedia Developer Audio/Video Editor Musician, Arranger, Composer www.georgelangley.ca Based upon code I found at [1] I have the following function [2] which I use to sort result sets. I've just added natural sorting (and natural caseless sorting) and some examples on usage. It might be slightly over the top, but it works well for me. No need to extract the columns from the rows to supply to the sorter. If you like it and find any issues with it, or enhancements, then I'd be grateful if you could pass them back to me. Richard. [1] http://www.php.net/manual/en/function.array-multisort.php#68452 [2] http://pastebin.com/8JsMX7yS -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_multisort into Natural order?
On 14 December 2010 10:50, Richard Quadling rquadl...@gmail.com wrote: On 13 December 2010 19:59, George Langley george.lang...@shaw.ca wrote: Hi all. Can use natsort($array1) to sort a single array of filenames into a natural alphanumeric order - 1.php, 2.php, 5.php, 10.php, 20.php, etc. But using array_multisort($array1, $array2, $array3) doesn't offer a natsort option, so I end up with 1.php, 10.php, 2.php, 20.php, 5.php, etc. Anyone have a solution to this limitation already? Nothing found in the archives or Googling. Thanks. George Langley Multimedia Developer Audio/Video Editor Musician, Arranger, Composer www.georgelangley.ca Based upon code I found at [1] I have the following function [2] which I use to sort result sets. I've just added natural sorting (and natural caseless sorting) and some examples on usage. It might be slightly over the top, but it works well for me. No need to extract the columns from the rows to supply to the sorter. If you like it and find any issues with it, or enhancements, then I'd be grateful if you could pass them back to me. Richard. [1] http://www.php.net/manual/en/function.array-multisort.php#68452 [2] http://pastebin.com/8JsMX7yS The function uses a closure for the sorting. If you are on PHP 5.3.0, then you'll have to extract the function into a normal one and add ... global $a_AMCOrdering; to each function. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_multisort into Natural order?
On 12/13/2010 11:59 AM, George Langley wrote: Hi all. Can use natsort($array1) to sort a single array of filenames into a natural alphanumeric order - 1.php, 2.php, 5.php, 10.php, 20.php, etc. But using array_multisort($array1, $array2, $array3) doesn't offer a natsort option, so I end up with 1.php, 10.php, 2.php, 20.php, 5.php, etc. Anyone have a solution to this limitation already? Nothing found in the archives or Googling. Thanks. assuming that you are not using other options of array_multisort(), why not do something like this. $ar = array_merge($array1, $array2, $array3); natsort($ar); George LangleyMultimedia DeveloperAudio/Video EditorMusician, Arranger, Composer www.georgelangley.ca -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_multisort into Natural order?
Hi there! Correct me if I'm wrong but merge() just makes one array of all items. array_multisort() keeps each array separate, while applying the same re-ordering to all three arrays. This allows you to use the same position number to get the required elements from each array, especially where each array is a list of different properties. For example: $name = array(Joe, Charlie, Jack); $age = array(30, 12, 66); $job = array(Plumber, Student, Retired); array_multisort($name, $age, $job); // will re-order all three arrays based on a regular alphanumeric sort of the names: // $name is now array(Charlie, Jack, Joe); // $age is now array(12, 66, 30); // $job is now array(Student, Retired, Plumber, ); // can now do: $firstName = $name[0]; // returns Charlie $firstAge = $age[0]; // returns Charlie's age 12 $firstJob = $job[0]; // returns Charlie's job Student A merge of these arrays will lose the different types of info each array currently has, and make it impossible to match a name to the age or job. Now, my other option is to group the items into a set of arrays: $array1 = array('name' = 'Joe', age = '30' job = 'Plumber'); $array2 = array('name' = 'Charlie', age = '12' job = 'Student'); $array3 = array('name' = 'Jack', age = '66' job = 'Retired'); $largeArray = array($array1, $array2, $array3); But, is there a way to the sort $largeArray, based on the name value in each individual array? And if so, can that use the natsort()? I don't think an asort() can do either. Note that the original source of this info is a mySQL db call, and it is initially sorted in the query, but again, there doesn't appear to be a way to natural sort an SQL query either. So I get file1, file10, file2, file20, file5 instead of file1, file2, file5, file10, file20 G - Original Message - From: Jim Lucas li...@cmsws.com Date: Monday, December 13, 2010 16:00 Subject: Re: [PHP] array_multisort into Natural order? To: George Langley george.lang...@shaw.ca Cc: php-general@lists.php.net On 12/13/2010 11:59 AM, George Langley wrote: Hi all. Can use natsort($array1) to sort a single array of filenames into a natural alphanumeric order - 1.php, 2.php, 5.php, 10.php, 20.php, etc. But using array_multisort($array1, $array2, $array3) doesn't offer a natsort option, so I end up with 1.php, 10.php, 2.php, 20.php, 5.php, etc. Anyone have a solution to this limitation already? Nothing found in the archives or Googling. Thanks. assuming that you are not using other options of array_multisort(), why not do something like this. $ar = array_merge($array1, $array2, $array3); natsort($ar); George Langley Multimedia Developer Audio/Video Editor Musician, Arranger, Composer www.georgelangley.ca George Langley Multimedia Developer Audio/Video Editor Musician, Arranger, Composer www.georgelangley.ca