• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

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

0
hankknight
Asked:
hankknight
1 Solution
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now