sorting a list of objects by value php
Posted
by
Mike
on Stack Overflow
See other posts from Stack Overflow
or by Mike
Published on 2014-06-09T21:21:46Z
Indexed on
2014/06/09
21:24 UTC
Read the original article
Hit count: 239
I have a list of objects:
class Beer {
var $name;
var $id;
var $style;
var $brewery;
var $rate;
function getName() {
return $this->name;
}
function getID() {
return $this->id;
}
function getStyle() {
return $this->style;
}
function getBrewery() {
return $this->brewery;
}
function getRate() {
return $this->rate;
}
}
After doing some research online on how to accomplish this, this is my code:
usort($localBeersList, "cmp");
function cmp($a, $b)
{
if ($a->getRate() == $b->getRate()) {
return 0;
}
return ($a->getRate() < $b->getRate()) ? -1 : 1;
}
If I try and output my list after this I do not get anything.
© Stack Overflow or respective owner