diego-ninja/sorter

通过多个字段对数组和对象进行排序,支持Unicode字符


README

Latest Version on Packagist Total Downloads PHP Version wakatime License Scrutinizer Code Quality Build Status Code Coverage

  1. 安装
  2. 使用
  3. 贡献
  4. 作者

安装

使用Composer进行安装

composer require diego-ninja/sorter

您可以通过运行以下命令来测试库(假设您有phpunit命令)

phpunit ./tests

使用

使用默认设置进行排序

use Ninja\Sorter\Sorter;

$data = array('ccc', 'aaa', 'bbb');
$sorter = new Sorter();
$data = $sorter->sort($data);
print_r($data); 

// prints array('aaa', 'bbb', 'ccc');

使用特定区域设置进行排序

UnicodeCIComparator(不区分大小写)比较器是此库以及默认情况下创建时使用的默认比较器,默认情况下它使用当前系统区域设置(来自php.ini)。

值得注意的是,当使用此比较器时,它可能会产生对数字的看起来很奇怪的结果。例如,-1000 大于 -100。如果您想按实际值比较数字,请使用 NumericComparator

use Ninja\Sorter\Comparator\UnicodeCIComparator;
use Ninja\Sorter\Sorter;
use Ninja\Sorter\Strategy\SimpleSortStrategy;

$strategy = new SimpleSortStrategy();
$strategy->setComparator(new UnicodeCIComparator('pl_PL'));
$sorter = new Sorter($strategy);
$sorter->sort(...);

保持键完整性的数组排序

use Ninja\Sorter\Sorter;
use Ninja\Sorter\Strategy\SimpleSortStrategy;

$array = array(0 => 'a', 1 => 'c', 2 => 'b');

$strategy = new SimpleSortStrategy();
$strategy->setPreserveKeys(true);

$sorter = new Sorter($strategy);
$sorter->sort($array);

print_r($array); // prints array(0 => 'a', 2 => 'b', 1 => 'c')

排序复杂对象

use Ninja\Sorter\Sorter;
use Ninja\Sorter\Strategy\ComplexSortStrategy;

$data = array(
    (object)array('name' => 'Ann', 'position' => '3', 'rating' => '3'),
    (object)array('name' => 'Ann', 'position' => '2', 'rating' => '2'),
    (object)array('name' => 'Ann', 'position' => '2', 'rating' => '1'),
    (object)array('name' => 'Betty', 'position' => '1', 'rating' => '2'),
);

$strategy = new ComplexSortStrategy();
$strategy
    ->setSortOrder(Sorter::ASC)
    ->sortBy('position')                                    // sort by position
    ->sortBy('name')                                        // sort by name if position is equal
    ->sortBy(function($object){return $object->rating})     // sort by rating if name is equal
;

$sorter = new src\Sorter();
$data = $sorter->setStrategy($strategy)->sort($data);

print_r($data);

//    prints:
//
//    Array
//    (
//        [0] => stdClass Object
//        (
//            [name] => Betty
//            [position] => 1
//            [rating] => 2
//        )
//
//        [1] => stdClass Object
//        (
//            [name] => Ann
//            [position] => 2
//            [rating] => 1
//        )
//
//        [2] => stdClass Object
//        (
//            [name] => Ann
//            [position] => 2
//            [rating] => 2
//        )
//
//        [3] => stdClass Object
//        (
//            [name] => Ann
//            [position] => 3
//            [rating] => 3
//        )
//    )

自定义

您可以为自己更复杂的数据集创建自己的策略。提供的 ComplexSortStrategy 应该能满足您的需要,如果不能满足,请尝试使用自己的比较器。您可以替换整个策略的默认比较器,或只为特定属性定义自己的比较器。

$strategy
    ->setSortOrder(Sorter::ASC)
    ->sortBy('position')
    ->sortBy('name', Sorter::DESC, new MyOwnPropertyComparator())
    ->sortBy('rating')
;

// or set your own comparator

$strategy->setComparator(new MyOwnPropertyComparator());

贡献

所有代码贡献都必须通过拉取请求。
将项目分支,创建一个功能分支,并发送给我一个拉取请求。

作者

此库受到了https://github.com/graze/sort的启发。