Re: array_multisort into Natural order?
| From: | Richard Quadling | Date: | Tue, 14 Dec 2010 10:52:46 +0000 |
| Subject: | Re: array_multisort into Natural order? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-310022@lists.php.net to get a copy of this message | ||
On 14 December 2010 10:50, Richard Quadling <rquadling@gmail.com> wrote:
> On 13 December 2010 19:59, George Langley <george.langley@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