Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting array from text file by date

Status
Not open for further replies.

csbdeady

Programmer
May 18, 2002
119
GB
Hi there

I've a text file like this:

a
b
02/02/2002
d
e
01/01/2001

I can load this into an array using file() but I then want (assuming this is a good way to do this?) to sort that array by the date, thus want to put it into a multidimensional array.

Basically every three lines in the original array is 1 record.

With the above example how can I get two records in an array sorted by date, ie:

record 1 = d,e,01/01/2001
record 2 = a,b,02/02/2002

Unfortunately the format of the text files is fixed.

Hope that makes sense!
Thanks
-Colin
 
I would loop through, reading a line at a time. Have a buffer that consists of two elements, which will hold the non-date values.

I would use an associative array for storing the values. Convert the dates fo YYYY/MM/DD for storage, and use them as the keys of the array. Dates that are in YYYY/MM/DD format are in date order when they are in alphabetical order.

Then use ksort() ( to sort the array. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top