PHPackages                             sgh/comparable-arrays - 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. sgh/comparable-arrays

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

sgh/comparable-arrays
=====================

Provides generic Comparators for arrays.

v1.0.2(11y ago)011MITPHPPHP &gt;=5.4.0

Since Apr 5Pushed 11y ago1 watchersCompare

[ Source](https://github.com/schmengler/comparable-arrays)[ Packagist](https://packagist.org/packages/sgh/comparable-arrays)[ Docs](https://github.com/schmengler/comparable-arrays)[ RSS](/packages/sgh-comparable-arrays/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

Comparable-Arrays
=================

[](#comparable-arrays)

[![Author](https://camo.githubusercontent.com/2e77af773c3c248fbc46eea4a88a1f2328a3d05f48b5e902a58db98ae9055de4/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d40667363686d656e676c65722d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/fschmengler)[![Latest Version](https://camo.githubusercontent.com/7b7def4f071eb8c88ab736800b30523270a574c7add8bcef04bb1d015de066bd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7367682f636f6d70617261626c652d6172726179732e737667)](https://packagist.org/packages/sgh/comparable-arrays)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/b1f5f6ca674d76d4ee2256d6598d42b46024863919ca63a18ca6847d8c251a84/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7363686d656e676c65722f636f6d70617261626c652d6172726179732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/schmengler/comparable-fileystem)

This package provides Comparators for arrays and objects that implement the SPL `ArrayAccess` interface. They can be used with the sorting and comparing tools in the [Comparable](https://github.com/schmengler/Comparator-Tools) package.

Requirements
------------

[](#requirements)

The package requires PHP 5.4 or later and the Comparable package in version 1.0 or later.

Install
-------

[](#install)

Via Composer

```
$ composer require sgh/comparable-arrays
```

Usage
-----

[](#usage)

The following comparators are available:

- `KeyComparator`: Use array item with specific key for comparison
- `MultiKeyComparator`: The same, but with the option to sort by multiple keys (i.e. fall back in case of equality)

You can use all the methods in `\SGH\Comparable\SortFunctions` and `\SGH\Comparable\SetFunctions` with the comparators.

### KeyComparator Example

[](#keycomparator-example)

```
use SGH\Comparable\SortFunctions;
use SGH\Comparable\Comparator\StringComparator;
use SGH\Comparable\Arrays\Comparator\KeyComparator;

$arrayOfBooks = array(
    [ 'title' => 'Design Patterns', 'author' => 'Gang of Four', 'year' => 1995 ],
    [ 'title' => 'Clean Code', 'author' => 'Uncle Bob', 'year' => 2008 ],
    [ 'title' => 'Refactoring', 'author' => 'Martin Fowler', 'year' => 1999 ],
    [ 'title' => 'Patterns of Enterprise Application Architecture', 'autor' => 'Martin Fowler', 'year' => 2002 ],
);

// Sort the array of books by year:
SortFunctions::sort($arrayOfBooks, new KeyComparator('year'));

// Sort the array of books by title:
SortFunctions::sort($arrayOfBooks, new KeyComparator('title', new StringComparator));
```

The default comparator used to compare the array items is `NumericComparator`, which compares any scalar values and treats them as numbers. To sort by title we needed to specify the comparator explicitly.

If you prefer, you can also use the factory method `::callback()` to retrieve a comparison callback, that can be used in any function that expects a user defined comparison callback:

```
usort($arrayOfBooks, KeyComparator::callback('title', new StringComparator));

```

### MultiKeyComparator Example

[](#multikeycomparator-example)

```
use SGH\Comparable\SortFunctions;
use SGH\Comparable\Comparator\ReverseComparator;
use SGH\Comparable\Comparator\StringComparator;
use SGH\Comparable\Arrays\Comparator\MultiKeyComparator;

$arrayOfBooks = array(
    [ 'title' => 'Design Patterns', 'author' => 'Gang of Four', 'year' => 1995 ],
    [ 'title' => 'Clean Code', 'author' => 'Uncle Bob', 'year' => 2008 ],
    [ 'title' => 'Refactoring', 'author' => 'Martin Fowler', 'year' => 1999 ],
    [ 'title' => 'Patterns of Enterprise Application Architecture', 'autor' => 'Martin Fowler', 'year' => 2002 ],
);

// Sort the array of books by author, then by year, descending:
SortFunctions::sort($arrayOfBooks, new MultiKeyComparator([
	'author' => new StringComparator,
	'year'   => new ReverseComparator(new NumericComparator)
]));
```

### Configuration

[](#configuration)

Both, `KeyComparator` and `MultiKeyComparator` accept arrays and objects that implement `ArrayAccess` as parameters. This default behavior can be changed such that they only accept arrays:

```
$comparator->setAcceptArrayAccessObject(false);
```

By default array keys must exist to compare their values. Both comparators have a non-strict mode, where missing items are treated as smaller than everything else. Two missing items are treated as equal.

```
$comparator->setStrict(false);
```

Testing
-------

[](#testing)

```
$ phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- Fabian Schmengler()
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 36% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

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

Total

2

Last Release

4082d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/be7ce3a8a91df7dcf5f545b805865f65cab4b96daedfd3bc20a3b792d2450467?d=identicon)[schmengler](/maintainers/schmengler)

---

Tags

comparatorarrayscomparable

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sgh-comparable-arrays/health.svg)

```
[![Health](https://phpackages.com/badges/sgh-comparable-arrays/health.svg)](https://phpackages.com/packages/sgh-comparable-arrays)
```

###  Alternatives

[ilya/belt

A handful of tools for PHP developers.

71020.9k1](/packages/ilya-belt)[phootwork/lang

Missing PHP language constructs

1227.1M8](/packages/phootwork-lang)[minwork/array

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

63263.9k5](/packages/minwork-array)[sgh/comparable

Provides Comparable and Comparator interfaces and methods to sort and compare objects based on these.

1474.5k2](/packages/sgh-comparable)[dragon-code/benchmark

Simple comparison of code execution speed between different options

12036.2k6](/packages/dragon-code-benchmark)[sarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

21182.2k1](/packages/sarhan-php-flatten)

PHPackages © 2026

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