Solved

# PHP: Sort Array Ascending With or Without Trailing Zeros

Posted on 2012-08-30
481 Views
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
)
``````
0
Question by:hankknight

LVL 25

Accepted Solution

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) {
if (\$a == \$b) {
return 0;
}
return (\$a < \$b) ? -1 : 1;
}
preg_match('/^(\w+)(\d+)(\.\w+)\$/', \$a, \$bits);
return sprintf("%s%05d%s",\$bits[1],\$bits[2],\$bits[3]);
}
``````
0

LVL 107

Expert Comment

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

LVL 49

Expert Comment

``````\$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;
}
``````
0

LVL 16

Author Comment

0

LVL 107

Expert Comment

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

As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.