https://github.com/diego-ninja/sorter
Sort arrays and objects by multiple fields with unicode characters
https://github.com/diego-ninja/sorter
satis-enabled
Last synced: 7 months ago
JSON representation
Sort arrays and objects by multiple fields with unicode characters
- Host: GitHub
- URL: https://github.com/diego-ninja/sorter
- Owner: diego-ninja
- License: bsd-3-clause
- Created: 2023-06-05T20:16:22.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-18T09:35:50.000Z (about 2 years ago)
- Last Synced: 2024-12-28T10:38:06.502Z (over 1 year ago)
- Topics: satis-enabled
- Language: PHP
- Homepage:
- Size: 277 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Sorter
[](https://packagist.org/packages/diego-ninja/sorter)
[](https://packagist.org/packages/diego-ninja/sorter)

[](https://wakatime.com/badge/user/bd65f055-c9f3-4f73-92aa-3c9810f70cc3/project/faea46b2-0902-4284-ab88-d971fe3b3390)
[](https://packagist.org/packages/diego-ninja/sorter)
[](https://scrutinizer-ci.com/g/diego-ninja/sorter/?branch=main)
[](https://scrutinizer-ci.com/g/diego-ninja/sorter/build-status/main)
[](https://scrutinizer-ci.com/g/diego-ninja/sorter/?branch=main)
1. [Installation](#installation)
2. [Usage](#usage)
- [Sort using default settings](#sort-using-default-settings)
- [Sort using a specific locale](#sort-using-a-specific-locale)
- [Sorting arrays keeping the keys intact](#sorting-arrays-keeping-the-keys-intact)
- [Sorting complex objects](#sorting-complex-objects)
- [Customizing](#customizing)
3. [Contributing](#contributing)
4. [Authors](#authors)
## Installation
Installation is done using [Composer](https://getcomposer.org/):
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
```php
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, use `NumericComparator`.
```php
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
```php
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
```php
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:
```php
$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](https://github.com/graze/sort).
- Jacek Kobus -
- Diego Rin -