glob() sorting

I am trying to sort the files in a glob("files/*.*") import ... but Im not able to sort  the result by name ... anyone know how to do this?


Folder with text files:

2011.01.25 - Tue, 25 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.28 - Fri, 28 Jan 2011.txt
Hans Inge HagenOwner and programmerAsked:
Who is Participating?
 
Beverley PortlockConnect With a Mentor Commented:
"It seems that I will need to make an Array of the $files to be able to asort/arsort the list ..."

glob returns an array, that's why I coded it the way I did.
0
 
DanDauchyCommented:
have you tried places each name in an array and then sorting the array w/ asort:

http://www.php.net/manual/en/function.asort.php
0
 
Beverley PortlockCommented:
If the names are as you have shown them and yur want them in increasing date sequence then this will do it

$arr = glob("files/*.*");
asort( $arr );
print_r( $arr );

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Hans Inge HagenOwner and programmerAuthor Commented:
This is what I have:

$files = glob("content/$dir/*.*"); for ($i=0; $i<count($files); $i++) { $num = $files[$i];
0
 
Beverley PortlockCommented:
Try this...

$files = glob("content/$dir/*.*");

asort( $files );

for ($i=0; $i<count($files); $i++) {
    $num = $files[$i];
    ..... etc
0
 
Hans Inge HagenOwner and programmerAuthor Commented:
I did try that with no success.

Maybe, there are some way to sort the files serverside? .. a script or something?
0
 
Beverley PortlockCommented:
What order are you trying to sort them in to? The example you gave above was already sorted in date order.
0
 
Hans Inge HagenOwner and programmerAuthor Commented:
It is sorted as:

2011.01.25 - Tue, 25 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.28 - Fri, 28 Jan 2011.txt

I need it to be:

2011.01.28 - Fri, 28 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.25 - Tue, 25 Jan 2011.txt

in the output. This is a diary where the latest text based file should be on top. 8)
0
 
Beverley PortlockCommented:
Use arsort instead of asort.
0
 
Hans Inge HagenOwner and programmerAuthor Commented:
It seems that I will need to make an Array of the $files to be able to asort/arsort the list ...
0
 
Hans Inge HagenOwner and programmerAuthor Commented:
When I "echo $files" the result writes  "Array" .. I dont know ..
0
 
Hans Inge HagenOwner and programmerAuthor Commented:
Thanks
0
 
Beverley PortlockCommented:
You cannot echo an array. Use print_r instead

print_r( $files );

0
 
Hans Inge HagenOwner and programmerAuthor Commented:
Great. The files is now sortet correctly ... the problem might be in the file_get_content("$num");

source:

$files = glob("content/$dir/*.*");

arsort($files);
print_r( $files );

for ($i=0; $i<count($files); $i++) {
$num = $files[$i];

$str= file_get_contents("$num");
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.