vujsa 0 Report post Posted October 9, 2006 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. <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: $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: Sunday789.htm Monday123.htm Monday321.htm Monday6321.htm Tuesday741.htm Tuesday4556.htm Wednesday12.htm Wednesday654.htm Thursday963.htm Friday52.htm Friday987.htm Saturday852.htm [/hr] 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 Xisto. 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. Share this post Link to post Share on other sites
BuffaloHelp 24 Report post Posted October 9, 2006 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 Share this post Link to post Share on other sites
vujsa 0 Report post Posted October 10, 2006 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.comOf 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:$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. Share this post Link to post Share on other sites
electron 0 Report post Posted October 12, 2006 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. Share this post Link to post Share on other sites
vujsa 0 Report post Posted October 12, 2006 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 Share this post Link to post Share on other sites