Originally posted by Gasolene
still doesn't seem to sort, the table ($this->rowArray) is the same b4 and after
note: it is inside a class
if it's got to be inside a class, then you more likely want to use the oop'ish version (the ColumnComparator class). it can be easily convinced either to fit inside another class or to hold one, with a little php-fu.
see, most of the problem with the version you're trying to use, is that it wasn't designed to be a member function. it's rather anti-oop, and the lack of $this->'s in the function apparently make it sort a nonexistent, empty array. Obviously, that doesn't help much. 🙂 I went to edit the function in order to oop'ize it, and wound up with code that looks an awful lot like the ColumnComparator class from a few days ago..
btw, the ColumnComparator was tested before i posted it. i know there's no problem with it. 🙂
If you want to bare-bones it, then you already have a var $rowArray in your class. Add a var $column, and a cmp($a, $b) function that looks like
function cmp($a, $b)
{
$val1 = $a[$this->column]; $val2 = $b[$this->column];
if ($val1 == $val2) return 0;
else return ($val1 > $val2) ? 1 : -1;
}
That function will be used as the callback for the usort() function, which uses a user-defined comparison function to decide what comes before what. The basic idea of the function, is that it compares two array elements (which, in this case, are arrays themselves) by looking at element [$column] of each one. (Each row should be treated as an element unto itself, for sorting purposes, else we could easily forget what we're doing and sort one column without moving the rest around.
Unless you like your variables butt-naked and exposed to all the freaks in php-land, you'll also want to build a sortBy($col) function to hide the details of how you're keeping your array sorted.
function sortBy($col)
{
$this->column = $col;
usort($this->rowArray, array(&$this, 'cmp'));
}