tags:

views:

75

answers:

2

Hi to all,

I can get a sorted filename list with the following code:

$log_files = scandir(LLP_LOG_DIR);
$sorted = sort($log_files);

the filename format is X.log, where X is a progressive numeric value.

How can I solve the problem of getting

0.log
1.log
10.log
11.log
2.log
3.log

where the wanted result is

0.log
1.log
2.log
3.log
[..]
9.log
10.log
11.log
[..]

I can strip the ".log" string, sort them etc, but what is the most efficient way?

+5  A: 

try natsort instead,

natsort($log_files)
jnunn
+1  A: 

Set the second parameter of sort to SORT_NUMERIC to have numeric sorting:

$sorted = $log_files;
sort($sorted, SORT_NUMERIC);

And note that sort sorts the array of the variable given with the first parameter. The return value of sort is just a boolean value.

Gumbo