diego-ninja / sorter
Sort arrays and objects by multiple fields with unicode characters
Fund package maintenance!
Ko Fi
paypal.me/diegorin
www.buymeacoffee.com/diegoninja
Requires
- php: ^8.2
- ext-bcmath: *
- ext-intl: *
- ext-mbstring: *
Requires (Dev)
- captainhook/captainhook: ^5.10
- captainhook/plugin-composer: ^5.3
- ergebnis/composer-normalize: ^2.30
- friendsofphp/php-cs-fixer: ^3.40
- icanhazstring/composer-unused: ^0.8.9
- maglnet/composer-require-checker: ^4.6
- mockery/mockery: ^1
- moxio/captainhook-psalm: ^0.3.0
- overtrue/phplint: ^9.0
- php-parallel-lint/php-console-highlighter: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10
- ramsey/conventional-commits: ^1.3
- roave/security-advisories: dev-latest
- scrutinizer/ocular: ^1.9
- dev-main
- 1.0.1
- 1.0.0
- dev-dependabot/composer/phpunit/phpunit-10.5.19
- dev-dependabot/composer/friendsofphp/php-cs-fixer-3.54.0
- dev-dependabot/composer/phpstan/phpstan-1.10.67
- dev-dependabot/composer/captainhook/captainhook-5.23.0
- dev-dependabot/composer/maglnet/composer-require-checker-4.11.0
- dev-dependabot/composer/php-parallel-lint/php-parallel-lint-1.4.0
- dev-dependabot/composer/mockery/mockery-1.6.11
- dev-dependabot/composer/overtrue/phplint-9.1.2
- dev-dependabot/composer/ergebnis/composer-normalize-2.42.0
This package is auto-updated.
Last update: 2024-11-18 11:08:53 UTC
README
Installation
Installation is done using Composer:
composer require diego-ninja/sorter
You can test the library using phpunit
by running the following command (assuming that you have phpunit
command available):
phpunit ./tests
Usage
Sort using default settings
use Ninja\Sorter\Sorter; $data = array('ccc', 'aaa', 'bbb'); $sorter = new Sorter(); $data = $sorter->sort($data); print_r($data); // prints array('aaa', 'bbb', 'ccc');
Sort using a specific locale
UnicodeCIComparator
(case-insensitive) comparator is the default comparator used in this library and by default during creation it uses current system locale (from php.ini).
It's worth to notice that when using this comparator, it may produce odd-looking results for numbers. For example
-1000
is greater than-100
. If you want to compare numbers by their real value, useNumericComparator
.
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(...);
Sorting arrays keeping the keys intact
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')
Sorting complex objects
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 // ) // )
Customizing
You can create your own strategies for more complicated data sets.
Provided ComplexSortStrategy
should cover most of your needs, and if it does not, try using your own comparators.
You can replace default comparators for a whole strategy or define your own only for specific properties:
$strategy ->setSortOrder(Sorter::ASC) ->sortBy('position') ->sortBy('name', Sorter::DESC, new MyOwnPropertyComparator()) ->sortBy('rating') ; // or set your own comparator $strategy->setComparator(new MyOwnPropertyComparator());
Contributing
All code contributions must go through a pull request.
Fork the project, create a feature branch, and send me a pull request.
Authors
This library was inspired by https://github.com/graze/sort.
- Jacek Kobus - kobus.jacek@gmail.com
- Diego Rin - yosoy@diego.ninja