Forum Moderators: coopster

Message Too Old, No Replies

sorting help

         

MrGecko

8:35 pm on Jul 16, 2007 (gmt 0)

10+ Year Member



i need help with sorting
this is the way this is what is contained in my array
1 = ¦S¦7/1/07¦S¦
2 = ¦S¦7/8/07¦S¦
3 = ¦S¦6/24/07¦S¦
4 = ¦S¦7/1/07¦S¦
5 = ¦S¦7/14/07¦S¦
6 = ¦S¦7/15/07¦S¦

i used sort function and this is what came out
1 = ¦S¦6/24/07¦S¦
2 = ¦S¦7/1/07¦S¦
3 = ¦S¦7/1/07¦S¦
4 = ¦S¦7/14/07¦S¦
5 = ¦S¦7/15/07¦S¦
6 = ¦S¦7/8/07¦S¦

i want it to be in this order
1 = ¦S¦6/24/07¦S¦
2 = ¦S¦7/1/07¦S¦
3 = ¦S¦7/1/07¦S¦
4 = ¦S¦7/8/07¦S¦
5 = ¦S¦7/14/07¦S¦
6 = ¦S¦7/15/07¦S¦

how can i do this?

Mr. Gecko

mattclayb

8:25 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



If you have control over the original values you need to reformat them for example -

instead of ¦S¦7/8/07¦S¦ it needs to be ¦S¦7/08/07¦S¦

coopster

10:12 pm on Jul 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Where are those values coming from? A database table? If so, format as they come out of the database in separate columns in the result set and your job will be much easier.

StudioKraft

4:17 am on Jul 18, 2007 (gmt 0)

10+ Year Member



Hello,

Sorting dates can be much easier if you use the timestamp instead of the formatted date, and then convert it back to the date format when you need it to be human-readable.

In other words, sorting:

12345678
12345987
12346120
etc.

is easier than sorting

1/2/07
12/23/06
etc.

Hope this helps,

SK

MrGecko

11:41 am on Jul 18, 2007 (gmt 0)

10+ Year Member



Sorry i was not on for a while i use a file database.
What function do i use to convert it back?
or do i convert it back by echo "$data[1][1]$data[1][2]/$data[1][3]$data[1][4]/$data[1][5]$data[1][6]";

StudioKraft

2:31 pm on Jul 19, 2007 (gmt 0)

10+ Year Member



Hello,

You should take your current array and make a new array with timestamps instead of dates:

$timeStamp = strtotime($data[# of date field]);

Sort the new array on the timestamp field and then you can convert it back using:

$data[# of data field] = date("m/d/Y",$timeStamp);

Hope this makes sense to you.