PHPackages                             phpextra/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. phpextra/sorter

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

phpextra/sorter
===============

Sort arrays and objects by multiple fields with unicode characters

1.0.1(10y ago)2969.2k2[1 issues](https://github.com/phpextra/sorter/issues)BSD-3-ClausePHPPHP &gt;=5.3

Since Jan 12Pushed 10y ago2 watchersCompare

[ Source](https://github.com/phpextra/sorter)[ Packagist](https://packagist.org/packages/phpextra/sorter)[ Docs](https://github.com/phpextra/sorter)[ RSS](/packages/phpextra-sorter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

\#Sorter [![Latest Stable Version](https://camo.githubusercontent.com/4f018133e6729ae3483dba300b1c0a6f8a31ee859eeccf91f02bcf2dfaf13628/68747470733a2f2f706f7365722e707567782e6f72672f70687065787472612f736f727465722f762f737461626c652e737667)](https://packagist.org/packages/phpextra/sorter)[![Total Downloads](https://camo.githubusercontent.com/2f54e6ced8e97b7632d97ef3c6ad5973bbccf60878fabeb6b3877debcbe9e1d1/68747470733a2f2f706f7365722e707567782e6f72672f70687065787472612f736f727465722f646f776e6c6f6164732e737667)](https://packagist.org/packages/phpextra/sorter)[![License](https://camo.githubusercontent.com/ea38afa4386b08f31be430d100bf80ba5265ba701687cc885d5fd6b67d9ea936/68747470733a2f2f706f7365722e707567782e6f72672f70687065787472612f736f727465722f6c6963656e73652e737667)](https://packagist.org/packages/phpextra/sorter)[![Build Status](https://camo.githubusercontent.com/bdbe39a9cc70271bfff2d8f250b38b0719b111670ea21691892eff3d5a645527/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f70687065787472612f736f727465722e737667)](https://travis-ci.org/phpextra/sorter)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/29ceac200c19549bb1ac31d168e92996124d8ceb53fbcbda6fbeffef84b0097b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f70687065787472612f736f727465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/phpextra/sorter/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/f160f7d13bc2650b91e5b3f356c748d91df4909b306a28b08416b3612e400f0d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f70687065787472612f736f727465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/phpextra/sorter/?branch=master)

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

```
use PHPExtra\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`.

```
use PHPExtra\Sorter\Sorter;
use PHPExtra\Sorter\Strategy\SimpleSortStrategy;
use PHPExtra\Sorter\Comparator\UnicodeCIComparator;

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

\###Sorting arrays keeping the keys intact

```
use PHPExtra\Sorter\Sorter;
use PHPExtra\Sorter\Strategy\SimpleSortStrategy;
use PHPExtra\Sorter\Comparator\UnicodeCIComparator;

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

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

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

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

\###Sorting complex objects

```
use PHPExtra\Sorter\Sorter;
use PHPExtra\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 PHPExtra\Sorter\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 .

Jacek Kobus -

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

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 ~183 days

Total

3

Last Release

3779d ago

Major Versions

1.0.1 → 2.0.x-dev2016-01-12

PHP version history (2 changes)1.0.0PHP &gt;=5.3.0

1.0.1PHP &gt;=5.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1527096?v=4)[Jacek](/maintainers/jkobus)[@jkobus](https://github.com/jkobus)

---

Top Contributors

[![jkobus](https://avatars.githubusercontent.com/u/1527096?v=4)](https://github.com/jkobus "jkobus (29 commits)")

---

Tags

arraysortcollectionspsr-0objectssorter

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/phpextra-sorter/health.svg)](https://phpackages.com/packages/phpextra-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)
