基于2个参数对对象数组进行分组和排序

时间:2011-02-26 14:08:19

标签: php sorting grouping usort

我有一个对象数组,我想根据一个属性进行排序,然后根据另一个属性进行“组合在一起”。在下面的示例中,我希望它们根据$sequence进行排序,并根据$artist进行分组。

//我班级的精简版:

<?php
class myClass
{
    public $sequence;
    public $artist;

    function __construct($sequence,$artist)
    {
        $this->sequence=$sequence;
        $this->artist=$artist;
    }

    static function cmp_myclass_sequence($a, $b)
    {
        $a_seq = $a->sequence;
        $b_seq = $b->sequence;
        if ($a_seq == $b_seq) {
            return 0;
        }
        return ($a_seq > $b_seq) ? +1 : -1;
    }
    static function cmp_myclass_artist($a, $b)
    {
        $a_art = strtolower($a->artist);
        $b_art = strtolower($b->artist);
        if ($a_art == $b_art) {
            return 0;
        }
        return ($a_art > $b_art) ? +1 : -1;
    }
}

//some objects of my class which am using for testing :

$myObj1 = new myClass(1,'A');
$myObj2 = new myClass(1,'B');
$myObj3 = new myClass(1,'C');
$myObj4 = new myClass(2,'A');
$myObj5 = new myClass(2,'B');
$myObj6 = new myClass(2,'C');
$myObj7 = new myClass(4,'A');
$myObj8 = new myClass(5,'A');
$myObj9 = new myClass(3,'B');
$myObj10 = new myClass(3,'G');
$myObj11= new myClass(3,'A');

$myArr=array($myObj1,$myObj2,$myObj3,$myObj4,$myObj5,$myObj6,$myObj7,$myObj8,$myObj9,$myObj10,$myObj11);

echo "My Array Before sort : <br>";
foreach ($myArr as $obj){ print_r($obj); echo "<br>"; }

usort($myArr, array("myClass", "cmp_myclass_sequence"));
//usort($myArr, array("myClass", "cmp_myclass_artist"));


echo "My Array AFTER sort : <br>";
foreach ($myArr as $obj){ print_r($obj); echo "<br>";}

?>

我在课堂上的排序功能cmp_myclass_sequencecmp_myclass_artist都可以自行运行。但我不知道如何以这种方式分组数组: 所有A艺术家都先出现,然后是B等等。 在A艺术家中,对象应该按该对象的序列int进行排序。

总之,这是我正在寻找的结果:

/*
My Array AFTER sort : 

myClass Object ( [sequence] => 1 [artist] => A )
myClass Object ( [sequence] => 2 [artist] => A )
myClass Object ( [sequence] => 3 [artist] => A )
myClass Object ( [sequence] => 4 [artist] => A )
myClass Object ( [sequence] => 5 [artist] => A )
myClass Object ( [sequence] => 1 [artist] => B )
myClass Object ( [sequence] => 2 [artist] => B )
myClass Object ( [sequence] => 3 [artist] => B )
myClass Object ( [sequence] => 1 [artist] => C )
myClass Object ( [sequence] => 2 [artist] => C )
myClass Object ( [sequence] => 3 [artist] => G ) 
*/

3 个答案:

答案 0 :(得分:4)

您可以使用ouzo goodies

执行此操作
$result = Arrays::sort($arr, Comparator::compound(
    Comparator::compareBy('artist'),
    Comparator::compareBy('sequence')
));

http://ouzo.readthedocs.org/en/latest/utils/comparators.html

答案 1 :(得分:2)

您可以使用一个比较功能轻松完成此操作:

function sort_c_arr($item_1, $item_2)
{
   if($item_1->artist != $item_2->artist)
   {
      return strcmp($item_1->artist, $item_2->artist);
   }
   return $item_1->sequence - $item_2->sequence;
}
// ...
usort($arr, 'sort_c_arr');

答案 2 :(得分:0)

比较函数怎么样:

function cmp_myclass_artist_then_sequence($a, $b) {
    $a_art = strtolower($a->artist);
    $b_art = strtolower($b->artist);
    if ($a_art == $b_art) {
        return cmp_myclass_sequence($as, $b);
    }
    return ($a_art > $b_art) ? +1 : -1;
}

这样你开始按艺术家进行比较,如果艺术家是相同的,你按顺序进行比较。我认为它应该有用。