Connecting Tech Pros Worldwide Forums | Help | Site Map

sorting numbers

Davide
Guest
 
Posts: n/a
#1: Aug 22 '06
Hi all..

I'm trying to sorting a file in a directory.
It works well but it sorts me the number in this way:

1
10
11
2
20
21
....

is there a way to sort in this way?
1
2
3
...
10
11
12

this is my code:

<?php
if ($handle = opendir('/home/davide/')) {
$files = array();
while (false !== ($file = readdir($handle))) {
if (($file != "." && $file != "..") AND (eregi($select,$file)))
$files[] = $file;
}
closedir($handle);
sort($files);
foreach($files as $file) {
$ext = substr(strrchr($file, "."), 1);
if ($ext == 'txt') {
$idx = str_replace('.txt', '', $file);
echo "$idx";
}
}
}
}
?>


Rik
Guest
 
Posts: n/a
#2: Aug 22 '06

re: sorting numbers


Davide wrote:
Quote:
Hi all..
>
I'm trying to sorting a file in a directory.
It works well but it sorts me the number in this way:
>
1
10
11
2
20
21
...
>
is there a way to sort in this way?
1
2
3
..
10
11
12
>
this is my code:
>
<?php
if ($handle = opendir('/home/davide/')) {
$files = array();
while (false !== ($file = readdir($handle))) {
if (($file != "." && $file != "..") AND
(eregi($select,$file))) $files[] = $file;
}
closedir($handle);
sort($files);
Change to natsort($files);

Grtz,
--
Rik Wasmus


Davide
Guest
 
Posts: n/a
#3: Aug 22 '06

re: sorting numbers


GREAT!!!
Thanx!

Rik ha scritto:
Quote:
Change to natsort($files);
>
Grtz,
--
Rik Wasmus
Closed Thread