PHP: Sort Array Ascending With or Without Trailing Zeros

I need to sort an array.  I tried using asort however I get this result:

Array (
    [0] => xa0.txt
    [1] => xa1.txt
    [2] => xa10.txt
    [3] => xa11.txt
    [4] => xa12.txt
    [5] => xa2.txt
    [6] => xa3.txt
    [7] => xa4.txt
    [8] => xa5.txt
    [9] => xa6.txt
    [10] => xa7.txt
    [11] => xa8.txt
    [12] => xa9.txt
)

Open in new window

LVL 16
hankknightAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lwadwellCommented:
the lack of leading zeroes on the numbers cause this.  You could do a custom sort using 'usort' ... e.g. if the string/filename will have that format consistently:
$arr = Array ( 'xa0.txt', 'xa1.txt', 'xa10.txt', 'xa11.txt', 'xa12.txt', 'xa2.txt', 'xa3.txt', 'xa4.txt', 'xa5.txt', 'xa6.txt', 'xa7.txt', 'xa8.txt', 'xa9.txt');
print_r($arr);

usort($arr, 'cmp');
print_r($arr);

function cmp($a, $b) {
    $a = zeroadd($a);
    $b = zeroadd($b);
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}
function zeroadd($a) {
    preg_match('/^(\w+)(\d+)(\.\w+)$/', $a, $bits);
    return sprintf("%s%05d%s",$bits[1],$bits[2],$bits[3]);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Please tell us which you think is a greater number.

xa1.txt
xa10.txt

The data seems to start with 'xa' followed by a number, followed by '.txt'

Is this a pattern we can depend on?  How many numbers will we expect between 'xa' and the dot (period)?

Functions you might use would include explode(), str_pad().  These would give you a fighting chance to normalize the file names.  With normalized names you can sort the data.  You might be able to create associative keys that can help you sort the names.
http://us.php.net/manual/en/array.sorting.php
0
Julian HansenCommented:
If your names all start with xa then to modify lwadwell's post
$arr = Array ( 'xa0.txt', 'xa1.txt', 'xa10.txt', 'xa11.txt', 'xa12.txt', 'xa2.txt', 'xa3.txt', 'xa4.txt', 'xa5.txt', 'xa6.txt', 'xa7.txt', 'xa8.txt', 'xa9.txt');
print_r($arr);

usort($arr, 'cmp');
print_r($arr);

function cmp($a, $b) {
  // Remove the chars and only compare the digits.
  return (intval(preg_replace('/[^0-9]/','', $a)) < intval(preg_replace('/[^0-9]/','', $b))) ? -1 : 1;
}

Open in new window

0
hankknightAuthor Commented:
0
Ray PaseurCommented:
To future EE visitors who might happen upon this question, there is a simple and built-in PHP function that accomplishes this objective without the necessity of added code.
http://php.net/manual/en/function.natsort.php

To learn about the PHP sorting functions you can read the online man page here:
http://php.net/manual/en/array.sorting.php
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.