Pergunta
O que é uma maneira elegante de classificar objetos em PHP? Eu adoraria fazer algo semelhante a isto.
$sortedObjectArary = sort($unsortedObjectArray, $Object->weight);
Basicamente especificar a matriz Quero tipo, bem como o campo Quero classificar. Olhei em matriz multidimensional de classificação e pode haver algo de útil lá, mas eu não vejo nada elegante ou óbvio.
Solução
Quase verbatim do manual:
function compare_weights($a, $b) {
if($a->weight == $b->weight) {
return 0;
}
return ($a->weight < $b->weight) ? -1 : 1;
}
usort($unsortedObjectArray, 'compare_weights');
Se você quiser objetos para ser capaz de classificar-se, ver exemplo 3 aqui: http://php.net/usort
Outras dicas
Para php> = 5,3
function osort(&$array, $prop)
{
usort($array, function($a, $b) use ($prop) {
return $a->$prop > $b->$prop ? 1 : -1;
});
}
Note que esta usos anônimos funções / encerramentos. Pode encontrar a revisão dos docs PHP no que útil.
Você pode até mesmo construir o comportamento de classificação para a classe que você está classificando, se você quiser que o nível de controle
class thingy
{
public $prop1;
public $prop2;
static $sortKey;
public function __construct( $prop1, $prop2 )
{
$this->prop1 = $prop1;
$this->prop2 = $prop2;
}
public static function sorter( $a, $b )
{
return strcasecmp( $a->{self::$sortKey}, $b->{self::$sortKey} );
}
public static function sortByProp( &$collection, $prop )
{
self::$sortKey = $prop;
usort( $collection, array( __CLASS__, 'sorter' ) );
}
}
$thingies = array(
new thingy( 'red', 'blue' )
, new thingy( 'apple', 'orange' )
, new thingy( 'black', 'white' )
, new thingy( 'democrat', 'republican' )
);
print_r( $thingies );
thingy::sortByProp( $thingies, 'prop1' );
print_r( $thingies );
thingy::sortByProp( $thingies, 'prop2' );
print_r( $thingies );
Para que a função de comparar, você pode apenas fazer:
function cmp( $a, $b )
{
return $b->weight - $a->weight;
}
A função de usort ( http://uk.php.net/manual /en/function.usort.php ) é seu amigo. Algo como ...
function objectWeightSort($lhs, $rhs)
{
if ($lhs->weight == $rhs->weight)
return 0;
if ($lhs->weight > $rhs->weight)
return 1;
return -1;
}
usort($unsortedObjectArray, "objectWeightSort");
Note que as chaves de matriz serão perdidas.
Você pode usar o usort () função e fazer a sua própria função de comparação.
$sortedObjectArray = usort($unsortedObjectArray, 'sort_by_weight');
function sort_by_weight($a, $b) {
if ($a->weight == $b->weight) {
return 0;
} else if ($a->weight < $b->weight) {
return -1;
} else {
return 1;
}
}
Dependendo do problema que você está tentando resolver, você também pode encontrar as interfaces SPL útil. Por exemplo, implementar a interface ArrayAccess lhe permitiria acessar sua classe como um array. Além disso, implementar a interface SeekableIterator iria deixá-lo percorrer o objeto como uma matriz. Desta forma, você pode classificar o seu objeto como se fosse uma matriz simples, tendo total controle sobre os valores que retorna para uma determinada chave.
Para mais detalhes:
function PHPArrayObjectSorter($array,$sortBy,$direction='asc')
{
$sortedArray=array();
$tmpArray=array();
foreach($this->$array as $obj)
{
$tmpArray[]=$obj->$sortBy;
}
if($direction=='asc'){
asort($tmpArray);
}else{
arsort($tmpArray);
}
foreach($tmpArray as $k=>$tmp){
$sortedArray[]=$array[$k];
}
return $sortedArray;
}
por exemplo =>
$myAscSortedArrayObject=PHPArrayObjectSorter($unsortedarray,$totalMarks,'asc');
$myDescSortedArrayObject=PHPArrayObjectSorter($unsortedarray,$totalMarks,'desc');
Você pode ter quase o mesmo código que você postou com classificadas função de NSPL :
use function \nspl\a\sorted;
use function \nspl\op\propertyGetter;
use function \nspl\op\methodCaller;
// Sort by property value
$sortedByWeight = sorted($objects, propertyGetter('weight'));
// Or sort by result of method call
$sortedByWeight = sorted($objects, methodCaller('getWeight'));
Se você quiser explorar a (aterrorizante) extensão de funções de estilo lambda em PHP, veja: http://docs.php.net/manual/en/function. criar-function.php