IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Sorting By The Day Of The Week

, An overview of a discussion.


vujsa
no avatar
Member [Level 2]
*****
Group: [MODERATOR]
Posts: 80
Joined: 25-March 05
Member No.: 4,879
myCENT:0.19



Post #1 post Oct 9 2006, 10:33 PM
Yesterday BuffaloHelp asked if I knew of a function or script that could sort filenames by the day of the week.

He has files with names like so:
Thursday567.htm
Wednesday345.htm
Monday123.htm
Friday789.htm
Sunday234.htm


If he reads the filenames from a directory and sorts the only by name then the will end up in this order:
Friday789.htm
Monday123.htm
Sunday234.htm
Thursday567.htm
Wednesday345.htm


The required result should be like so:
Sunday234.htm
Monday123.htm
Wednesday345.htm
Thursday567.htm
Friday789.htm


There isn't just some nice simple function or method to sort these based on the days of the week.

Here is the first version of a script I came up with for him.

CODE

<pre>
<?php
$days = array("Monday123.htm","Wednesday12.htm","Friday52.htm","Tuesday4556.htm",
"Sunday789.htm","Monday321.htm","Monday6321.htm","Wednesday654.htm",
"Friday987.htm","Tuesday741.htm","Saturday852.htm","Thursday963.htm");

$day_name = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$day_number = array('0','1','2','3','4','5','6');

$temp_array = str_replace($day_name, $day_number, $days);

array_multisort($temp_array, SORT_ASC, SORT_STRING, $days);

echo "Output Values:\n";
var_dump($days);

?>
</pre>


Basically, the script reads the array of filenames, creates a temporary array from the first but replaces the weekday name with a numeric value instead. Then the array of names is sort in the order that the temporary array would normally be sorted in.

I'll explain in detail:
We use the <pre> tag in order to use the preformated output the the PHP parser sends to the browser. Otherwise everything would try to be on the same line or wrap around lines instead of a nice column of information.

The $days variable holds the data for the array of filenames that would be generated by reading the directory contents. This is just all of the filenames in the directory. In this case, I manually created the array since I'm not going to actually read a file directory.

The $day_name variable holds the data for the array of weekday names. This is basically our search array meaning we will later search the $days array for these names.

The $day_number variable holds the data for the array of weekday numbers. This is basically our replace array meaning we will later replace the $days array with these numbers.

$day_name and $day_number are related to each other in that the Name in $day_name must correspond to the Number in $day_number.

We create $temp_array by substituing the $day_name in $days with the corresponding $day_number!

Now we should have something like this in our $temp_array:
CODE
$days = array("1123.htm","312.htm","552.htm","24556.htm","0789.htm","1321.htm","16321.htm",
"3654.htm","5987.htm","2741.htm","6852.htm","4963.htm");

In otherwords, Sunday becomes 0, Monday becomes 1, Tuesday becomes 2, etc...

array_multisort() is a powerful function that can sort multidiminsional arrays as well as sort one array based on the data in a second array!
With this function, we will sort the $days array with the values from the $temp_array array.
So if the 4th position in $temp_array should go to the 1st position, then the 4th position in $days should also go to the 1st position!

The end result is that the files are sorted in order based on which day of the week comes first.

The var_dump() function simply flushes all of the variable information about $days to the browser in raw form.

The end result should look like this:
QUOTE

Sunday789.htm
Monday123.htm
Monday321.htm
Monday6321.htm
Tuesday741.htm
Tuesday4556.htm
Wednesday12.htm
Wednesday654.htm
Thursday963.htm
Friday52.htm
Friday987.htm
Saturday852.htm


[highlight=lime][hr=0] [/hr][/highlight]

As I said, this is the first version of the script I wrote for BuffaloHelp. He has asked that I write about it so that he can add it to the code repository he is working on for Trap17.

The second, more powerful version, I'll convert to a function an submit it to my new PHP code repository for public use. I simply don't think that code should be posted in multiple places so I will only post it on one website. This script stays here and the other one in function form with be added to Handy PHP

I won't add it right away since there are a number of other issues I need to take care of first but it will get there soon

Enjoy.
Go to the top of the page
+Quote Post
BuffaloHelp
no avatar
More than meets the eye
******************
Group: Admin
Posts: 3,736
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042
myCENT:99.60



Post #2 post Oct 9 2006, 10:50 PM
This is a brilliant script! You have no idea how many times I have searched on the net... and believe it or not, this sorting method is highly sought after. There were theories but no working script.

Thanks vujsa! PHP online manual just doesn't cut it for me. This really makes CMS possible with my site. I don't have to monitor the file uploads by my league refs.

My script will have array() that will read all files within my schedule directory. This will replace your $days() array.

Since the topic is here, I can now place this code as working script on annex.trap17.com
Go to the top of the page
+Quote Post
vujsa
no avatar
Member [Level 2]
*****
Group: [MODERATOR]
Posts: 80
Joined: 25-March 05
Member No.: 4,879
myCENT:0.19



Post #3 post Oct 10 2006, 12:20 AM
QUOTE(BuffaloHELP @ Oct 9 2006, 06:50 PM) [snapback]287590[/snapback]

This is a brilliant script! You have no idea how many times I have searched on the net... and believe it or not, this sorting method is highly sought after. There were theories but no working script.

Thanks vujsa! PHP online manual just doesn't cut it for me. This really makes CMS possible with my site. I don't have to monitor the file uploads by my league refs.

My script will have array() that will read all files within my schedule directory. This will replace your $days() array.

Since the topic is here, I can now place this code as working script on annex.trap17.com

Of course the script would require adaptation to you specific need which is why I decided to convert the script to a function.

Then you could simply do the following to sort your $unsorted_array array:
CODE

$sorted_array = my_sort_function($unsorted_array);


Also, this particualr script doesn't allow for human error! You should add a validation routine to ensure that your refs are using the correct naming convention when uploading their files.

Just let me know if any bugs pop up but there isn't much room for bugs here. tongue.gif
Go to the top of the page
+Quote Post
electron
no avatar
Premium Member
********
Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375
myCENT:35.86



Post #4 post Oct 12 2006, 03:08 PM
I never understood that why did the files have the numbers at the end of the files what do the indicate.
Also why did you change all the days into numbers as their day numbers.
Is it some style of recording dates.
Go to the top of the page
+Quote Post
vujsa
no avatar
Member [Level 2]
*****
Group: [MODERATOR]
Posts: 80
Joined: 25-March 05
Member No.: 4,879
myCENT:0.19



Post #5 post Oct 12 2006, 03:51 PM
QUOTE(electron @ Oct 12 2006, 11:08 AM) [snapback]288140[/snapback]

I never understood that why did the files have the numbers at the end of the files what do the indicate.
Also why did you change all the days into numbers as their day numbers.
Is it some style of recording dates.

Well, the file names are examples only. If you hade several weeks worth of files, you may have 10 "Monday" files, 10 "Tuesday" files etc...

The file name isn't as important as being able to sort the data.

The day names are replaced with numbers to allow for sorting the days in the order they are in through the week.

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday is the correct order that the days appear in a week.

Friday, Monday, Saturday, Sunday, Tuesday, Thursday, Wednesday is the alphabetical order that normal sorting would place the day in.

In this case, the user wanted all of the Monday files together and wanted then to come after Sunday and befor Tuesday.

So we substituted the daynaes with numeric values like so:
Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6

Once the temporary array has been built with numbers instead of names, the original array is sorted by the order that the temporary array should be ordered. Thus placing the day names in order of occurance on the calendar.

As I understand the need addressed here, the user received data files with sports scores. The filename determines the content of the file. So Friday08GP.htm might mean somthing like The number eight game at Grant Park! If you wanted to have a website where all of the league scores were posted daily this would be a great way for your officials to update the website. The end user can view the scores in order of the days the games were played. So when displayed, the scored would be in a block for the week and each day 's scores would be group together. Otherwise, the Friday scores would always be at the top of the list and the Wednesday scores would always be tat the bottom.

I hope this better explains the way the script works and the task that it performs.

I have created a more flexable function version of this script. You can find it here: Handy PHP Functions : Array Sort By Day
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new   38 cateye999 26,133 14th May 2009 - 08:22 PM
Last post by: Tenshi
No New Posts   0 etms 718 22nd June 2006 - 12:45 PM
Last post by: etms
No New Posts   3 solankyno1 4,590 30th December 2004 - 04:31 PM
Last post by: wassie
No new   32 Hirishiolo 3,622 12th November 2008 - 10:48 PM
Last post by: joeblogg
No New Posts   2 kvarnerexpress 3,606 17th February 2005 - 09:54 PM
Last post by: kristofer.hoch
No New Posts   0 Neutrality 3,232 26th February 2005 - 12:15 AM
Last post by: Neutrality
No New Posts 9 bhavesh 1,222 20th June 2007 - 04:59 PM
Last post by: bhavesh
No new   20 husker 2,732 20th July 2006 - 01:05 AM
Last post by: icemarle
No New Posts   0 -davenom- 2,217 30th November 2004 - 07:39 AM
Last post by: -davenom-
No New Posts   0 truefusion 874 30th October 2005 - 06:12 PM
Last post by: truefusion
No New Posts   8 truefusion 1,432 6th November 2005 - 06:54 PM
Last post by: cmatcmextra
No New Posts   6 Raptrex 2,420 22nd May 2005 - 01:13 PM
Last post by: xerrax
No New Posts 6 A200 712 16th June 2008 - 12:47 PM
Last post by: buherath
No new   60 BeyondEarth 8,579 11th December 2007 - 02:40 AM
Last post by: jimmyln2004
No New Posts   5 moldboy 1,702 30th June 2005 - 05:02 PM
Last post by: HmmZ


 



RSS Open Discussion Time is now: 4th July 2009 - 12:10 AM

Web Hosting Powered by ComputingHost.com.