是否可以在PHP中使用usort按多个属性对对象进行排序


Is it possible to sort objects by multiple properties using usort in PHP?

我有一个有点像这样的对象:

<?php
class Change {
        private $prop1, $prop2, $prop3;
        public function __construct($prop1, $prop2, $prop3) {
                $this->$prop1=$prop1;
                $this->$prop2=$prop2;
                $this->$prop3=$prop3;
        }
        public function getProp1() {
                return $this->prop1;
        }
        public function getProp2() {
                return $this->prop2;
        }
        public function getProp3() {
                return $this->prop3;
        }
}
?>

我已经删除了一些细节,但这通常就是对象。现在,我想对顶部prop1不等于NULL的对象进行排序,然后按prop2的特定值进行排序。Prop2可以等于"High"、"Medium"、"Low"、"Critical"或用户输入的任何其他值。我想按以下顺序对它们进行排序:关键、高、中、低,以及其他所有内容。最后,我想按字母顺序对prop3进行排序。

这可能与usort有关吗?有没有更简单的方法?

排序顺序:

  • Prop1-不是NULL
  • Prop2-"临界"、"高"、"中"、"低"*
  • Prop3-字母
function sort($a,$b){
    $criteria = array('Critical'=>4,'High'=>3,'Medium'=>2,'Low'=>1);
    if($a.Prop1 != NULL && $b.Prop1 == NULL) return -1;
    if($criteria[$a.Prop2] != $criteria[$b.Prop2]) {
        if($criteria[$a.Prop2] < $criteria[$b.Prop2]){
            return -1;
        }else{
            return 1;
        }
    }
    return strcmp($a.Prop3,$b.Prop3);
}

对不起,我不测试它,但它必须以这种方式工作