PDA

View Full Version : PHP Array Sorting



LoneRaven
December 2nd, 2003, 03:28
I wrote a class to run an events calendar using a text file instead of a database. Before I rewrite the information back to the file I would like to sort it based in the date. However, the date is not the first key in the array. How would I go about doing such a sort.

Here is a short ouput from the print_r function to give you an idea of the array structure.

Array
(
[Event One] => events Object
(
[Event] => Event One
[Date] => 11/29/2003
[Description] => Description One
[Notes] => Notes One
[Deleted] => False
)

[Event Two] => events Object
(
[Event] => Event Two
[Date] => 11/29/2003
[Description] => Description Two
[Notes] => Notes Two
[Deleted] => False
)
)

Thanks for your help

Eagle
December 3rd, 2003, 06:07
I wrote a class to run an events calendar using a text file instead of a database. Before I rewrite the information back to the file I would like to sort it based in the date. However, the date is not the first key in the array. How would I go about doing such a sort.

Here is a short ouput from the print_r function to give you an idea of the array structure.

Array
(
[Event One] => events Object
(
[Event] => Event One
[Date] => 11/29/2003
[Description] => Description One
[Notes] => Notes One
[Deleted] => False
)

[Event Two] => events Object
(
[Event] => Event Two
[Date] => 11/29/2003
[Description] => Description Two
[Notes] => Notes Two
[Deleted] => False
)
)

Thanks for your help

OK, you can accomplish this in hundreads of ways but there are a few basic sort algorithms used in simple data sorts. They are extremely inefficient compared to other sort algorithms but they work none the less. The most common sort is a bubble sort. I could write one for you but I'm lazy and its late at night so here is an example I looked up, citations are all there. Of course it can be modified for classes just remember to think simple, dont try to swap all the data in the class's objects, just swap the event objects in the external array. Remember to change the comparisons to compare the date in the object though. I assume your a good enough programmer to trace through the code and figure this out so that should be enough explanation. If not, my Data Structures teacher lived by the motto, "Draw a Picture".



<?php
/* bubble.php by detour@metalshell.com
*
* Generate random numbers then sort them.
*
* http://www.metalshell.com/
*
*/


$array_size = 250;

// If you use v4.2.0 or lower uncomment this
// srand((double)microtime()*1000000);

// Generate $array_size random numbers to be sorted.
for($x = 0; $x < $array_size; $x++)
$ran[$x] = rand(0, 500);

/* The bubble sort method. If you don't know how it works it's very
* simple, values are switched one at a time for each element. */
for($x = 0; $x < $array_size; $x++) {
for($y = 0; $y < $array_size; $y++) {
if($ran[$x] < $ran[$y]) {
$hold = $ran[$x];
$ran[$x] = $ran[$y];
$ran[$y] = $hold;
}
}
}

for($x = 0; $x < $array_size; $x++)
print $ran[$x] . "<br>";


?>