Custom array sorting based on instance properties
Posted
by St. John Johnson
on Stack Overflow
See other posts from Stack Overflow
or by St. John Johnson
Published on 2010-03-22T00:08:49Z
Indexed on
2010/03/22
0:11 UTC
Read the original article
Hit count: 534
I'm trying to perform a usort
on an array inside an instance of a class. But the sort is dependent on the properties of said instance.
Code (which doesn't work):
class foo {
private $array;
private $key;
private $dir;
function sort() {
usort($this->array, array("foo", "orderArray"));
}
function orderArray($a, $b) {
return strcmp($a[$this->key], $b[$this->key]) * $this->dir;
}
}
From the orderArray
class, you can't access $key or $dir. The question is, how can I write this so I can?
© Stack Overflow or respective owner