hi,
I think you need the usort function. It allows you to define your own comparing function to sort your entry. But first you have to design your array properly. If you want to sort an array:
array[0] = $id;
array[1] = $desc;
array[2] = $date; (formated as MM/DD/YY);
doesn't mean anything is NOT correct
array [0] = array ("id" => $id, "desc" => $desc, "date" => $date);
because your array is an array of object and that object is defined by an id, a desc and a date. Am I right ? So it's an array of array. I used key to define an array name. If you know anything about DB you probably think that It's like a field name. An the array itself is a table with rows. No ?
so now the only think you have to it's to use the usort function:
void usort ( array array, string cmp_function)
it takes an array and a function name as arguments. That function returns "The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.".
Here is a sample from the PHP Manual:
function cmp ($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
So It has 2 arguments, a and b. Here each argument is an array! Yes because we have id, desc and date! So we just have to compare the date here!
function MyCmpDate ($a, $b)
{
if ($a ['date'] == $b ['date']) return 0;
return ($a ['date'] > $b ['date']) ? -1 : 1;
}
Actually I don't think you can compare a date in PHP, it's not a primitive type (like int, string...) so you will have to use a PHP Date Function. Hum... I just searched for it and it doesn't exist 🙂. Simply compare the dates by year, then by month, then by day. To get the d, m and y value from your date, simply use the explode function:
list ($y, $m, $d) = explode ("-", $date);
I let you compare $ay with $by... Year for date a and b... The same for month and day. But there's probably an easier way to compare a date in PHP.
Last but not least I wonder why you need such a trick to handle an array of object. Wouldn't it be easier to use a DB to store these objects ? You would simply have to query and order by your rows. No ?
SELECT * FROM MyTable ORDER BY date
and voilà 🙂
JM