How can I make my own custom class be sortable using sort() for example?
I’ve been scanning the web to find any method of making a class Comparable like in Java but without much luck. I tried implementing __equals() but without luck. I’ve also tried with __toString(). My class looks like this:
class Genre {
private $genre;
private $count;
...
}
I want to sort them by count which is an Integer, in descending order… ($genre is a string)
You can create a custom sort method and use the http://www.php.net/manual/en/function.usort.php function to call it.
Example:
If you’d like to make a more generic collection system you could try something like this