PHPackages                             diego-ninja/sorter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. diego-ninja/sorter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

diego-ninja/sorter
==================

Sort arrays and objects by multiple fields with unicode characters

1.0.1(2y ago)04[9 PRs](https://github.com/diego-ninja/sorter/pulls)BSD-3-ClausePHPPHP ^8.2

Since Jun 5Pushed 2y ago1 watchersCompare

[ Source](https://github.com/diego-ninja/sorter)[ Packagist](https://packagist.org/packages/diego-ninja/sorter)[ Docs](https://github.com/diego-ninja/sorter)[ Fund](https://paypal.me/diegorin)[ Fund](https://www.buymeacoffee.com/diegoninja)[ RSS](/packages/diego-ninja-sorter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (17)Versions (12)Used By (0)

Sorter
======

[](#sorter)

[![Latest Version on Packagist](https://camo.githubusercontent.com/961544d482cdfac635787f69fb54f1b2043f4306496284103a2f71d4478d203f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646965676f2d6e696e6a612f736f727465722e7376673f7374796c653d666c6174)](https://packagist.org/packages/diego-ninja/sorter)[![Total Downloads](https://camo.githubusercontent.com/3e215d95069049ce7b8f74dcb2acad3278f2a1cbda8f0b79d853ce76153621c8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646965676f2d6e696e6a612f736f727465722e7376673f7374796c653d666c617426636f6c6f723d626c7565)](https://packagist.org/packages/diego-ninja/sorter)[![PHP Version](https://camo.githubusercontent.com/0996f77aa9ca7ed8e60ca9931c732e33057872e5c2890a19754e765eab56f683/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f646965676f2d6e696e6a612f736f727465722e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/0996f77aa9ca7ed8e60ca9931c732e33057872e5c2890a19754e765eab56f683/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f646965676f2d6e696e6a612f736f727465722e7376673f7374796c653d666c6174)[![wakatime](https://camo.githubusercontent.com/a4c93f90e5e9ddc05b756e7e0bbcfdf03bf512271865889b862d12e3e08080e4/68747470733a2f2f77616b6174696d652e636f6d2f62616467652f757365722f62643635663035352d633966332d346637332d393261612d3363393831306637306363332f70726f6a6563742f66616561343662322d303930322d343238342d616238382d6439373166653362333339302e737667)](https://wakatime.com/badge/user/bd65f055-c9f3-4f73-92aa-3c9810f70cc3/project/faea46b2-0902-4284-ab88-d971fe3b3390)[![License](https://camo.githubusercontent.com/4e6de158f7a2738f0ab045f585a6b1bdaba3156ac922bbe4c138e8866b2440b1/68747470733a2f2f706f7365722e707567782e6f72672f646965676f2d6e696e6a612f736f727465722f6c6963656e73652e737667)](https://packagist.org/packages/diego-ninja/sorter)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/fe94d67acb81483f451843258c225bae6fabad20c670e4f892b049e76387b8d6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f646965676f2d6e696e6a612f736f727465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/diego-ninja/sorter/?branch=main)[![Build Status](https://camo.githubusercontent.com/e210985d4cf9a6ce6d26687222ecc4bc190f83162ba1ddbab5460eb45b5ab2ab/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f646965676f2d6e696e6a612f736f727465722f6261646765732f6275696c642e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/diego-ninja/sorter/build-status/main)[![Code Coverage](https://camo.githubusercontent.com/967d1014b240ba2d909507948e313171c2f8209a6d875f4e7b765465a9550e1b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f646965676f2d6e696e6a612f736f727465722f6261646765732f636f7665726167652e706e673f623d6d61696e)](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)

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
-----

[](#usage)

### Sort using default settings

[](#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

[](#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`.

```
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

[](#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

[](#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

[](#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
------------

[](#contributing)

All code contributions must go through a pull request.
Fork the project, create a feature branch, and send me a pull request.

Authors
-------

[](#authors)

This library was inspired by .

- Jacek Kobus -
- Diego Rin -

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~225 days

Total

2

Last Release

849d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dff5b0a76de3d555cb236c400384058ba23982bce2e859cc1b2f0c1f0161a3e6?d=identicon)[diego.ninja](/maintainers/diego.ninja)

---

Top Contributors

[![diego-ninja](https://avatars.githubusercontent.com/u/78662279?v=4)](https://github.com/diego-ninja "diego-ninja (4 commits)")

---

Tags

satis-enabledarraysortcollectionsPSR-4objectssorterphp82

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/diego-ninja-sorter/health.svg)

```
[![Health](https://phpackages.com/badges/diego-ninja-sorter/health.svg)](https://phpackages.com/packages/diego-ninja-sorter)
```

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k411.1M1.2k](/packages/doctrine-collections)[ilya/belt

A handful of tools for PHP developers.

71020.8k1](/packages/ilya-belt)[minwork/array

Pack of advanced array functions specifically tailored for: associative (assoc) array, multidimensional array, array of objects and handling nested array elements

66256.1k5](/packages/minwork-array)[vanderlee/php-stable-sort-functions

Class of stable sort methods. Equal values remain in the original order. Only different values are sorted.

33741.2k1](/packages/vanderlee-php-stable-sort-functions)[topshelfcraft/supersort

...a super-duper sorting function for your Craft templates.

4287.1k1](/packages/topshelfcraft-supersort)[graze/sort

A collection of array sorting transforms and functions

12289.6k2](/packages/graze-sort)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
