https://github.com/senseexception/intl-sort
A wrapper library for PHP Intl to sort values or objects according to local conventions
https://github.com/senseexception/intl-sort
Last synced: about 1 year ago
JSON representation
A wrapper library for PHP Intl to sort values or objects according to local conventions
- Host: GitHub
- URL: https://github.com/senseexception/intl-sort
- Owner: SenseException
- License: mit
- Created: 2020-03-02T10:16:12.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2025-01-27T20:55:25.000Z (over 1 year ago)
- Last Synced: 2025-03-14T09:05:45.995Z (about 1 year ago)
- Language: PHP
- Homepage: https://senseexception.github.io/intl-sort
- Size: 264 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# intl-sort
A wrapper library for PHP Intl to sort values/objects based on rules of locales.
This library wraps the Collator class of the PHP Intl extension and offers a more fluid API without constant juggling
for you to sort your values or objects by country/locale specific standards.
[](https://packagist.org/packages/senseexception/intl-sort)
[](https://packagist.org/packages/senseexception/intl-sort)


[](https://packagist.org/packages/senseexception/intl-sort)
### Why using this library?
PHP functions like `sort()` work most of the time when it comes to sort values,
but they don't return useful results when a country or language specific order of values is
needed like e.g. words with umlauts or other accents.
This library wraps the `Collator` class of the PHP Intl extension and offers a builder pattern
API to create a `Sorter` to sort values or value objects by the rules of a locale. You also can
implement your own sorting logic.
## Installation
You can install this with [Composer](https://getcomposer.org/).
```
composer require senseexception/intl-sort
```
### Documentation
Read more about what this library is capable of
in the [documentation](https://senseexception.github.io/intl-sort).
### Examples
While PHP's own sort functions don't order the elements in a way that is expected in different
countries, intl-sort will sort them with the help of the Intl-extension appropriate for the
countries in your international project.
#### Ascending order
```php
$sortBuilder = new \Budgegeria\IntlSort\Builder('de_DE');
$sorter = $sortBuilder->getSorter();
$sortedArray = $sorter->sort(['a', 'g', 'A', 'ß', 'ä', 'j', 'z']);
var_dump($sortedArray); // [0 => 'a', 2 => 'A', 4 => 'ä', 1 => 'g', 5 => 'j', 3 => 'ß', 6 => 'z'];
```
#### Descending order
```php
$sortBuilder = new \Budgegeria\IntlSort\Builder('de_DE');
$sorter = $sortBuilder->orderByDesc()->getSorter();
$sortedArray = $sorter->sort(['a', 'g', 'A', 'ß', 'ä', 'j', 'z']);
var_dump($sortedArray); // [0 => 'z', 1 => 'ß', 2 => 'j', 3 => 'g', 4 => 'ä', 5 => 'A', 6 => 'a',];
```
#### Order by keys
```php
$sortBuilder = new \Budgegeria\IntlSort\Builder('de_DE');
$sorter = $sortBuilder->orderByKeys()->getSorter();
$sortedArray = $sorter->sort(['g' => 1, 'A' => 2, 'ß' => 3, 'ä' => 4, 'z' => 5]);
var_dump($sortedArray); // ['A' => 2, 'ä' => 4, 'g' => 1, 'ß' => 3, 'z' => 5];
```
```php
$sortBuilder = new \Budgegeria\IntlSort\Builder('de_DE');
$sorter = $sortBuilder->orderByKeys()->orderByDesc()->getSorter();
$sortedArray = $sorter->sort(['g' => 1, 'A' => 2, 'ß' => 3, 'ä' => 4, 'z' => 5]);
var_dump($sortedArray); // ['z' => 5, 'ß' => 3, 'g' => 1, 'ä' => 4, 'A' => 2,];
```
There are also more configuration possibilities in the builder like setting strength,
lower case first / upper case first or special french collation.
## Does it affect [GDPR](https://www.eugdpr.org/) somehow?
intl-sort itself uses the locale only for the purposes to sort values with the help of the
PHP Intl extension.