dongsuyou6938 2009-10-06 23:05
浏览 47
已采纳

如何在PHP中对对象数组进行排序?

Say I have a group of people, each person having a name, sex, and age property, coded like this:

public class Person
{
   private $name, $sex, $age;
   public function Person ($name, $sex, $age)
   {
       $this->name = $name;
       $this->sex = $sex;
       $this->age = $age;
   }
   public function getName();    
   public function getSex();
   public function getAge();
}

public class People
{
   private $people = array();
   public function addPerson ($name, $sex, $age)
   {
      $this->people[] = new Person($name, $sex, $age);
   }
}

How could I implement a method sortPeople() which sorted the $people array in ascending order of the peoples' names to the People class?

  • 写回答

3条回答 默认 最新

  • dqwnxdhb88531 2009-10-07 00:00
    关注

    Here is a working code with a static method. It also uses the fact that the static method can access private ivars :) It also uses PHP awesome reflexivity <3.

    The good point about that code is that Person is the class to provide the sort method, which is better on an OOP point of view. Only the class Person should be the one to know how to sort other Person. Neither People or another indepent function should.

    Note: not using is_callable(), as it only verifies if the parameter can be called as a function, but does not check if it's actually callable with the current visibility (public, private, protected)

    class Person
    {
        private $name, $sex, $age;
        public function Person($name, $sex, $age)
        {
            $this->name = $name;
            $this->sex = $sex;
            $this->age = $age;
        }
    
        public static function sortByName(Person $p1, Person $p2)
        {
            return strcmp($p1->name, $p2->name);
        }
    
        public static function sortByAge(Person $p1, Person $p2)
        {
            return ($p1->age - $p2->age);
        }
    }
    
    class People
    {
        private $people = array();
        public function addPerson($name, $sex, $age)
        {
            $this->people[] = new Person($name, $sex, $age);
        }
    
        public function display()
        {
            print_r($this->people);
        }
    
        public function sort($attribute = 'name')
        {
            $sortFct = 'sortBy' . ucfirst(strtolower($attribute));
            if (!in_array($sortFct, get_class_methods('Person')))
            {
                throw new Exception('People->sort(): Can\'t sort by ' . $attribute);
            }
            usort($this->people, 'Person::' . $sortFct);
        }
    }
    
    $people = new People;
    $people->addPerson('Steve', 'M', 31);
    $people->addPerson('John', 'M', 24);
    $people->addPerson('Jane', 'F', 26);
    $people->addPerson('Sally', 'F', 21);
    $people->display();
    $people->sort();
    $people->display();
    $people->sort('age');
    $people->display();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料