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

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

rmtram/sorter
=============

Simple sort of multiple arrays.

v2.0.0(6y ago)28.7k↑196.7%MITPHPPHP &gt;=7.2CI failing

Since Apr 27Pushed 6y ago2 watchersCompare

[ Source](https://github.com/Rmtram/Sorter)[ Packagist](https://packagist.org/packages/rmtram/sorter)[ Docs](https://github.com/Rmtram/Sorter)[ RSS](/packages/rmtram-sorter/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (2)Versions (5)Used By (0)

[![Build Status](https://camo.githubusercontent.com/f09d9c3ffc453576b0c2c498e4503c74dd350e94a2ebd62aac1624db5b00042a/68747470733a2f2f7472617669732d63692e6f72672f526d7472616d2f536f727465722e737667)](https://travis-ci.org/Rmtram/Sorter)[![Latest Stable Version](https://camo.githubusercontent.com/c31a3b308db7753cb66fbd72e87ac2cf4ed209777450a2d66ee2722af437be2c/68747470733a2f2f706f7365722e707567782e6f72672f726d7472616d2f736f727465722f762f737461626c652e706e67)](https://packagist.org/packages/rmtram/sorter)[![Total Downloads](https://camo.githubusercontent.com/609cebdc3bb7f0d803271c6631a056211aa29c7023d1617cdfedd4b185fd1ef3/68747470733a2f2f706f7365722e707567782e6f72672f726d7472616d2f736f727465722f646f776e6c6f616473)](https://packagist.org/packages/rmtram/sorter)[![License](https://camo.githubusercontent.com/b24c9998c918975783d36446c2868787b6339debda95a5f9206d5eec2e8214e5/68747470733a2f2f706f7365722e707567782e6f72672f726d7472616d2f736f727465722f6c6963656e7365)](https://packagist.org/packages/rmtram/sorter)

Sorter
======

[](#sorter)

Simple sort of multiple arrays.

Install
=======

[](#install)

```
$ composer require rmtram/sorter

```

OR

[Source Download](https://github.com/Rmtram/Sorter/archive/v2.0.0.zip)

Copy file: `src/Sorter.php`

```
require '/path/to/Sorter.php';

```

Contents
========

[](#contents)

- \[static\] [make(array $items)](#make)
- \[static\] [runSort(array $items, array $orders, $select = \[\], $offset = null, $limit = null)](#runSort)
- [sort(array $orders)](#sort)
- [select(array $attributes)](#select)
- [offset(int|null $int = null)](#offset)
- [limit(int|null $int = null)](#limit)

Methods
=======

[](#methods)

make
----

[](#make)

- Create instance.

```
$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$sorter = Rmtram\Sorter\Sorter::make($items);
```

runSort
-------

[](#runsort)

```
$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::runSort($items, ['id' => 'asc'], ['id'], 1, 1);

var_dump($results);
```

- Result

```
array(1) {
  [0]=>
  array(1) {
    ["id"]=>
    int(2)
  }
}

```

sort
----

[](#sort)

### Single (id =&gt; asc)

[](#single-id--asc)

- Source code

```
$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::make($items)->sort(['id' => 'asc']);

var_dump($results);
```

- Result

```
array(4) {
  [0]=>
  array(3) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(3) "abc"
    ["created_at"]=>
    string(19) "2015-10-14 10:10:01"
  }
  [1]=>
  array(3) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(3) "def"
    ["created_at"]=>
    string(19) "2015-10-14 10:10:05"
  }
  [2]=>
  array(3) {
    ["id"]=>
    int(3)
    ["name"]=>
    string(3) "ghi"
    ["created_at"]=>
    string(19) "2015-10-14 10:10:09"
  }
  [3]=>
  array(3) {
    ["id"]=>
    int(5)
    ["name"]=>
    string(3) "mno"
    ["created_at"]=>
    string(19) "2015-10-14 10:10:39"
  }
}

```

### Multiple

[](#multiple)

- Source code

```
$items = [
    ['id' => 1, 'name' => 'b', 'age' =>  9, 'created_at' => '2015-10-10 10:10:00'],
    ['id' => 2, 'name' => 'a', 'age' =>  9, 'created_at' => '2015-10-10 10:10:10'],
    ['id' => 3, 'name' => 'z', 'age' =>  3, 'created_at' => '2015-10-10 10:10:20'],
    ['id' => 5, 'name' => 'f', 'age' => 11, 'created_at' => '2015-10-10 10:10:15'],
    ['id' => 4, 'name' => 'e', 'age' => 16, 'created_at' => '2015-10-10 10:10:20'],
    ['id' => 6, 'name' => 'o', 'age' => 15, 'created_at' => '2015-10-10 10:10:05']
];

$results = Rmtram\Sorter\Sorter::make($items)->sort([
    'age'        => 'asc',
    'created_at' => 'asc',
    'id'         => 'desc'
]);

var_dump($results);
```

- Result

```
array(6) {
  [0]=>
  array(4) {
    ["id"]=>
    int(3)
    ["name"]=>
    string(1) "z"
    ["age"]=>
    int(3)
    ["created_at"]=>
    string(19) "2015-10-10 10:10:20"
  }
  [1]=>
  array(4) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(1) "b"
    ["age"]=>
    int(9)
    ["created_at"]=>
    string(19) "2015-10-10 10:10:00"
  }
  [2]=>
  array(4) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(1) "a"
    ["age"]=>
    int(9)
    ["created_at"]=>
    string(19) "2015-10-10 10:10:10"
  }
  [3]=>
  array(4) {
    ["id"]=>
    int(5)
    ["name"]=>
    string(1) "f"
    ["age"]=>
    int(11)
    ["created_at"]=>
    string(19) "2015-10-10 10:10:15"
  }
  [4]=>
  array(4) {
    ["id"]=>
    int(6)
    ["name"]=>
    string(1) "o"
    ["age"]=>
    int(15)
    ["created_at"]=>
    string(19) "2015-10-10 10:10:05"
  }
  [5]=>
  array(4) {
    ["id"]=>
    int(4)
    ["name"]=>
    string(1) "e"
    ["age"]=>
    int(16)
    ["created_at"]=>
    string(19) "2015-10-10 10:10:20"
  }
}

```

select
------

[](#select)

### Single

[](#single)

- Source code

```
$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::make($items)->refuse('age')->sort(['id' => 'asc']);

var_dump($results);
```

- Result

```
array(4) {
  [0]=>
  array(1) {
    ["id"]=>
    int(1)
  }
  [1]=>
  array(1) {
    ["id"]=>
    int(2)
  }
  [2]=>
  array(1) {
    ["id"]=>
    int(3)
  }
  [3]=>
  array(1) {
    ["id"]=>
    int(5)
  }
}

```

### Multiple

[](#multiple-1)

- Source code

```
$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 1, 'name' => 'bac', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$sortedItems = Rmtram\Sorter\Sorter::make($items)->select(['id', 'name'])->sort(['id' => 'asc', 'name' => 'desc']);

var_dump($sortedItems);
```

- Result

```
array(5) {
  [0]=>
  array(2) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(3) "bac"
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(3) "abc"
  }
  [2]=>
  array(2) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(3) "def"
  }
  [3]=>
  array(2) {
    ["id"]=>
    int(3)
    ["name"]=>
    string(3) "ghi"
  }
  [4]=>
  array(2) {
    ["id"]=>
    int(5)
    ["name"]=>
    string(3) "mno"
  }
}

```

offset
------

[](#offset)

`Offset null === offset 0`

**!!! Important !!!**

Changed offset implementation.

> Before

```
offset(0) => 0
offset(1) => 0
offset(2) => 1
offset(3) => 2

```

> After

```
offset(0) => 0
offset(1) => 1
offset(2) => 2
offset(3) => 3

```

- Source code

```
$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::make($items)
    ->offset(3)
    ->sort(['id' => 'asc']);

var_dump($results);
```

- Result

```
array(1) {
  [0]=>
  array(3) {
    ["id"]=>
    int(5)
    ["name"]=>
    string(3) "mno"
    ["created_at"]=>
    string(19) "2015-10-14 10:10:39"
  }
}

```

limit
-----

[](#limit)

`No limit in the case of null`

- Source code

```
$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::make($items)
    ->limit(1)
    ->sort(['id' => 'asc']);

var_dump($results);
```

- Result

```
array(1) {
  [0]=>
  array(3) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(3) "abc"
    ["created_at"]=>
    string(19) "2015-10-14 10:10:01"
  }
}

```

Support versions.
=================

[](#support-versions)

- PHP
    - 7.2
    - 7.3
    - 7.4

LICENSE
=======

[](#license)

The MIT License (MIT)

Copyright (c) 2016 Rmtram

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 78.3% 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 ~444 days

Total

4

Last Release

2386d ago

Major Versions

v1.0.0 → v2.0.02019-12-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d7cbad47788700bb6b0fe45e8431c751d6be5d4cb7806895140c9e38f3bd7b3?d=identicon)[Rmtram](/maintainers/Rmtram)

---

Top Contributors

[![Rmtram](https://avatars.githubusercontent.com/u/15045107?v=4)](https://github.com/Rmtram "Rmtram (18 commits)")[![NoguHiro](https://avatars.githubusercontent.com/u/4655005?v=4)](https://github.com/NoguHiro "NoguHiro (5 commits)")

---

Tags

sortmulti sortmultiple sort

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[kyslik/column-sortable

Package for handling column sorting in Laravel 6.x

6625.9M25](/packages/kyslik-column-sortable)[mottie/tablesorter

tablesorter (FORK) is a jQuery plugin for turning a standard HTML table with THEAD and TBODY tags into a sortable table without page refreshes. tablesorter can successfully parse and sort many types of data including linked data in a cell.

2.6k232.2k](/packages/mottie-tablesorter)[icecave/parity

A customizable deep comparison library.

517.2M11](/packages/icecave-parity)[symbiote/silverstripe-gridfieldextensions

A collection of useful grid field components

951.9M271](/packages/symbiote-silverstripe-gridfieldextensions)[undefinedoffset/sortablegridfield

Adds drag and drop functionality to Silverstripe's GridField

911.3M52](/packages/undefinedoffset-sortablegridfield)[vanderlee/php-stable-sort-functions

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

33768.3k1](/packages/vanderlee-php-stable-sort-functions)

PHPackages © 2026

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