PHPackages                             phrity/comparison - 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. [Search &amp; Filtering](/categories/search)
4. /
5. phrity/comparison

ActiveLibrary[Search &amp; Filtering](/categories/search)

phrity/comparison
=================

Interfaces and helper trait for comparing objects. Comparator for sort and filter applications.

1.4.1(5mo ago)0163.4k↓10.2%2MITPHPPHP ^8.1CI passing

Since Jan 5Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/sirn-se/phrity-comparison)[ Packagist](https://packagist.org/packages/phrity/comparison)[ Docs](https://phrity.sirn.se/comparison)[ RSS](/packages/phrity-comparison/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (9)Used By (2)

[![Phrity Net Uri](docs/logotype.png)](docs/logotype.png)

[![Build Status](https://github.com/sirn-se/phrity-comparison/actions/workflows/acceptance.yml/badge.svg)](https://github.com/sirn-se/phrity-comparison/actions)[![Coverage Status](https://camo.githubusercontent.com/1437ff2493a275ed867ba9dc21b52f3578e331b02f4cab54a84f275cd2890aa5/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7369726e2d73652f7068726974792d636f6d70617269736f6e2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/sirn-se/phrity-comparison?branch=master)

Comparison
==========

[](#comparison)

Interfaces and helper trait for comparing objects. Comparator utility class for sort and filter applications.

Current version supports PHP `^7.1|^8.0`.

Installation
------------

[](#installation)

Install with [Composer](https://getcomposer.org/);

```
composer require phrity/comparison

```

The Equalable interface
-----------------------

[](#the-equalable-interface)

### Interface synopsis

[](#interface-synopsis)

```
interface Phrity\Comparison\Equalable {

    /* Abstract methods */
    abstract public equals(mixed $compare_with) : bool
}
```

### Examples

[](#examples)

```
// $a must implement Equalable, $b can be anything
$a->equals($b); // True if $a is equal to $b
```

The Comparable interface
------------------------

[](#the-comparable-interface)

Extends `Equalable` interface.

### Interface synopsis

[](#interface-synopsis-1)

```
interface Phrity\Comparison\Comparable
    extends Phrity\Comparison\Equalable {

    /* Abstract methods */
    abstract public greaterThan(mixed $compare_with) : bool
    abstract public greaterThanOrEqual(mixed $compare_with) : bool
    abstract public lessThan(mixed $compare_with) : bool
    abstract public lessThanOrEqual(mixed $compare_with) : bool
    abstract public compare(mixed $compare_with) : int

    /* Inherited from Equalable */
    abstract public equals(mixed $compare_with) : bool
}
```

### Examples

[](#examples-1)

```
// $a must implement Comparable, $b can be anything
$a->greaterThan($b);        // True if $a is greater than $b
$a->greaterThanOrEqual($b); // True if $a is greater than or equal to $b
$a->lessThan($b);           // True if $a is less than $b
$a->lessThanOrEqual($b);    // True if $a is less than or equal to $b

//  0 if $a is equal to $b
// -1 if $a is less than $b
//  1 if $a is greater than $b
$a->compare($b);
```

The ComparisonTrait trait
-------------------------

[](#the-comparisontrait-trait)

A class using this trait only has to implement the `compare()` method. Enables all other methods in `Equalable` and `Comparable` intefaces.

### Trait synopsis

[](#trait-synopsis)

```
trait Phrity\Comparison\ComparisonTrait
    implements Phrity\Comparison\Comparable {

    /* Methods */
    public equals(mixed $compare_with) : bool
    public greaterThan(mixed $compare_with) : bool
    public greaterThanOrEqual(mixed $compare_with) : bool
    public lessThan(mixed $compare_with) : bool
    public lessThanOrEqual(mixed $compare_with) : bool

    /* Inherited from Comparable */
    abstract public compare(mixed $compare_with) : int
}
```

The Comparator class
--------------------

[](#the-comparator-class)

Utility class to sort and filter array objects that implement the `Comparable` interface.

### Class synopsis

[](#class-synopsis)

```
class Phrity\Comparison\Comparator {

    /* Methods */
    public __construct(array $comparables = null)
    public sort(array $comparables = null) : array
    public rsort(array $comparables = null) : array
    public equals(Comparable $condition, array $comparables = null) : array
    public greaterThan(Comparable $condition, array $comparables = null) : array
    public greaterThanOrEqual(Comparable $condition, array $comparables = null) : array
    public lessThan(Comparable $condition, array $comparables = null) : array
    public lessThanOrEqual(Comparable $condition, array $comparables = null) : array
    public min(array $comparables = null) : Comparable
    public max(array $comparables = null) : Comparable
}
```

### Examples

[](#examples-2)

```
$comparables = [$v2, $v1, $v4, $v3];
$condition = $v3;
$comparator = new Comparator();

// Sort and reverse sort array of Comparable
$comparator->sort($comparables); // [$v1, $v2, $v3, $v4]
$comparator->rsort($comparables); // [$v4, $v3, $v2, $v1]

// Filter array of Comparable using a Comparable as condition
$comparator->equals($condition, $comparables); // [$v3]
$comparator->greaterThan($condition, $comparables); // [$v4]
$comparator->greaterThanOrEqual($condition, $comparables); // [$v4, $v3]
$comparator->lessThan($condition, $comparables); // [$v2, $v1]
$comparator->lessThanOrEqual($condition, $comparables); // [$v2, $v1, $v3]

// Select min/max instance
$comparator->min($comparables); // $v1
$comparator->max($comparables); // $v4

// Can also "store" comparables for re-use in multiple operations
$comparables = [$v2, $v1, $v4, $v3];
$comparator = new Comparator($comparables);
$comparator->sort(); // [$v1, $v2, $v3, $v4]
$comparator->equals($condition); // [$v3]
$comparator->min(); // $v1
```

The IncomparableException class
-------------------------------

[](#the-incomparableexception-class)

Must be thrown if comparison methods receive input they can not compare with.

### Class synopsis

[](#class-synopsis-1)

```
class Phrity\Comparison\IncomparableException
    extends InvalidArgumentException {

    /* Inherited from InvalidArgumentException */
    public __construct([string $message = '' [, int $code = 0 [, Throwable $previous = null]]])
    public getMessage() : string
    public getPrevious() : Throwable
    public getCode() : mixed
    public getFile() : string
    public getLine() : int
    public getTrace() : array
    public getTraceAsString() : string
    public __toString() : string
}
```

Versions
--------

[](#versions)

VersionPHP`1.4``^8.1``1.3``^7.1|^8.0``1.2``^7.1`The `Comparator` supports stored content for multiple operations`1.1``>=5.6`The `Comparator` class for sort and filter`1.0``>=5.6``Equalable` and `Comparable` interface, `ComparisonTrait` trait

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance71

Regular maintenance activity

Popularity34

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity77

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

Recently: every ~595 days

Total

8

Last Release

165d ago

PHP version history (4 changes)1.0.0PHP &gt;=5.6

1.2.0PHP ^7.1

1.3.0PHP ^7.1|^8.0

1.4.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4255391?v=4)[Sören Jensen](/maintainers/sirn-se)[@sirn-se](https://github.com/sirn-se)

---

Top Contributors

[![sirn-se](https://avatars.githubusercontent.com/u/4255391?v=4)](https://github.com/sirn-se "sirn-se (22 commits)")

---

Tags

comparablecomparatorcomparisonequalablefilterphpphp-librarysortcomparatorfiltersortcomparisoncomparableequalable

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phrity-comparison/health.svg)

```
[![Health](https://phpackages.com/badges/phrity-comparison/health.svg)](https://phpackages.com/packages/phrity-comparison)
```

###  Alternatives

[clue/stream-filter

A simple and modern approach to stream filtering in PHP

1.7k261.7M7](/packages/clue-stream-filter)[laminas/laminas-filter

Programmatically filter and normalize data and files

9528.0M150](/packages/laminas-laminas-filter)[kitpages/data-grid-bundle

Symfony DataGridBundle

7780.9k1](/packages/kitpages-data-grid-bundle)

PHPackages © 2026

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