How to sort an array of objects in PHP?

Let's say I have a group of people, each of whom has a name, gender and age property, encoded as follows:

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()that sorted an array $peoplein ascending order of the names of people in a class People?

+3
source share
3 answers

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

, Person - , , . Person , Person. People, .

: is_callable(), , , , (, , )

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();
+8

usort. . , , , , ( ). , , Person .

( ) . , - :

class People {
    // your previously defined stuff here...

    public function sort() {
        usort($this->people, array($this, 'comparePeople'));
    }

    public function comparePeople(Person $p1, Person $p2) {
        return strcmp($p1->getName(), $p2->getName());
    }
}

, , getName() Person.

:

function sortPeople($people) {
    usort($people, array('People', 'comparePeople'));
}

class People {
    // your previously defined stuff here...

    public static function comparePeople(Person $p1, Person $p2) {
        return strcmp($p1->getName(), $p2->getName());
    }
}

, . . .

+4

, getName() , decorate-sort-undecorate.

<?php

class Person {
    private $name;
    function getName() {
      return $this->name;
    }
    function __construct($name) {
      $this->name = $name;
    }
}

$people = array(
    new Person('Jim'),
    new Person('Tom'),
    new Person('Tim'),
    new Person('Adam')
);

// actual sorting below
$people = array_map(create_function('$a', 'return array($a->getName(), $a);'), $people); // transform array of objects into array of arrays consisted of sort key and object
sort($people); // sort array of arrays
$people = array_map('end', $people); // take only last element from each array

print_r($people);

?

, , - , . .

sort , PHP , .

, , , , . , :

$people = array_map(create_function('$a', 'return array($a->getSurname(), $a->getName(), $a);'), $people); 

This method can be faster if used usort, because it calls getName () only n times to sort an array of length n. Comparison during sorting is performed using the built-in comparator, so it should be fast. In the method, a usortcustom comparator is called several times (more than n times) during sorting, and this can slow down the work.

+1
source

Source: https://habr.com/ru/post/1719465/


All Articles