Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

glob() sorting

Posted on 2011-02-26
14
580 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:Hans Inge Hagen
  • 7
  • 6
14 Comments
 
LVL 4

Expert Comment

by:DanDauchy
ID: 34989314
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34991976
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
 

Author Comment

by:Hans Inge Hagen
ID: 34992083
This is what I have:

$files = glob("content/$dir/*.*"); for ($i=0; $i<count($files); $i++) { $num = $files[$i];
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995628
Try this...

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

asort( $files );

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

Author Comment

by:Hans Inge Hagen
ID: 34995756
I did try that with no success.

Maybe, there are some way to sort the files serverside? .. a script or something?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995813
What order are you trying to sort them in to? The example you gave above was already sorted in date order.
0
 

Author Comment

by:Hans Inge Hagen
ID: 34995853
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995871
Use arsort instead of asort.
0
 

Author Comment

by:Hans Inge Hagen
ID: 34995930
It seems that I will need to make an Array of the $files to be able to asort/arsort the list ...
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 50 total points
ID: 34996026
"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
 

Author Comment

by:Hans Inge Hagen
ID: 34996043
When I "echo $files" the result writes  "Array" .. I dont know ..
0
 

Author Closing Comment

by:Hans Inge Hagen
ID: 34996081
Thanks
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34996149
You cannot echo an array. Use print_r instead

print_r( $files );

0
 

Author Comment

by:Hans Inge Hagen
ID: 34996223
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question