PHPackages                             the-jj/spl-collections-sort - 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. the-jj/spl-collections-sort

ActiveLibrary

the-jj/spl-collections-sort
===========================

Helper library for sorting Spl data structures

1.0.3(8y ago)11.2kMITPHPPHP &gt;=5.6

Since Aug 25Pushed 8y ago1 watchersCompare

[ Source](https://github.com/the-JJ/SplCollectionsSort)[ Packagist](https://packagist.org/packages/the-jj/spl-collections-sort)[ RSS](/packages/the-jj-spl-collections-sort/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (5)Used By (0)

SPL collections sort
====================

[](#spl-collections-sort)

[![Latest Stable Version](https://camo.githubusercontent.com/3c53faadcf5b1afdd4039d7cc7a0dd1e8b62ef5ecab50a55a743a254da85a7e6/68747470733a2f2f706f7365722e707567782e6f72672f7468652d6a6a2f73706c2d636f6c6c656374696f6e732d736f72742f76657273696f6e)](https://packagist.org/packages/the-jj/spl-collections-sort)[![Latest Unstable Version](https://camo.githubusercontent.com/d89f8b9c9e44398ae1a13f727815411f8c4911ad605b8afe80648cf5187a7e0f/68747470733a2f2f706f7365722e707567782e6f72672f7468652d6a6a2f73706c2d636f6c6c656374696f6e732d736f72742f762f756e737461626c65)](//packagist.org/packages/the-jj/spl-collections-sort)[![Build Status](https://camo.githubusercontent.com/b4e67d36185f5e6ad4c28b50e49159e50944a72dbaf8f6ee76498e9c8b0d0f4f/68747470733a2f2f7472617669732d63692e6f72672f7468652d4a4a2f53706c436f6c6c656374696f6e73536f72742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/the-JJ/SplCollectionsSort)[![Coverage Status](https://camo.githubusercontent.com/8e24ff655bf8a5b833c1f6d05048e14a5cc437bdd46ab5021ac94eacbbe95657/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7468652d4a4a2f53706c436f6c6c656374696f6e73536f72742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/the-JJ/SplCollectionsSort?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/75550fa4caddbba571e2f541471c84f3b83acf9f037035f666c290b11a81b158/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f30373830316532612d343938302d343539662d613661392d6532626431376437366466382f6d696e692e706e67)](https://insight.sensiolabs.com/projects/07801e2a-4980-459f-a6a9-e2bd17d76df8)[![License](https://camo.githubusercontent.com/8a81a80ab882e61967a546d88da4044dbc0af03fa98e855e5b939bfa90ab2e47/68747470733a2f2f706f7365722e707567782e6f72672f7468652d6a6a2f73706c2d636f6c6c656374696f6e732d736f72742f6c6963656e7365)](https://packagist.org/packages/the-jj/spl-collections-sort)

Several methods for sorting SPL datastructures. Currently only `SplFixedArray` objects are supported. Should be fast with big arrays as well.

Basic usage
===========

[](#basic-usage)

All sorting algorithms accept `SplFixedArray` object as first argument and optional comparison callback function as second argument. Sorting is done in place.

- [Insertion sort](#insertion-sort)
- [Quicksort](#quicksort)
- [Mergesort](#mergesort)
- [Fallback to standard PHP array `sort()`](#array-sort)

Insertion sort
--------------

[](#insertion-sort)

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::insertionSort($splFixedArray);

var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]
```

#### Custom comparison function

[](#custom-comparison-function)

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::insertionSort($splFixedArray, function($a, $b) {
    if ($a < $b) return 1;
    if ($a > $b) return -1;
    return 0;
});

var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]
```

#### Boundaries

[](#boundaries)

Additionally, insertion sort accepts two more arguments - boundaries `$low` and `$high`:

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::insertionSort($splFixedArray, null, 2, 4);

var_dump($splFixedArray->toArray()); // [5, 3, 0, 6, 8, 4]
```

Quicksort
---------

[](#quicksort)

Non-recursive implementation is used, which makes it viable for sorting big arrays. Upon reaching subsets of 5 elements or less, falls back to insertion sort.

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::quickSort($splFixedArray);

var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]
```

#### Custom comparison function

[](#custom-comparison-function-1)

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::quickSort($splFixedArray, function($a, $b) {
    if ($a < $b) return 1;
    if ($a > $b) return -1;
    return 0;
});

var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]
```

Mergesort
---------

[](#mergesort)

A bottom-up (non-recursive) implementation is used.

Usage? You guessed it:

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::mergeSort($splFixedArray);

var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]
```

#### Custom comparison function

[](#custom-comparison-function-2)

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::mergeSort($splFixedArray, function($a, $b) {
    if ($a < $b) return 1;
    if ($a > $b) return -1;
    return 0;
});

var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]
```

Array sort
----------

[](#array-sort)

There is one more sorting method - *array sort* that uses PHP's `sort()` (or `usort()`) method. Usage is same as with the above algorithms:

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::arraySort($splFixedArray);

var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]
```

#### Custom comparison function

[](#custom-comparison-function-3)

```
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::arraySort($splFixedArray, function($a, $b) {
    if ($a < $b) return 1;
    if ($a > $b) return -1;
    return 0;
});

var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]
```

Why?
====

[](#why)

When using a regular PHP array, we are offered a vast array *(no pun intended)* of [sorting functions](https://secure.php.net/manual/en/array.sorting.php). However, no such thing is offered for `SplFixedArray` objects in vanilla PHP.

This *library* is to change that. Currently it offers several methods for sorting `SplFixedArray` objects. In the future, I might add support for other structures (that make sense to be sorted, like doubly-linked list?).

Installation
============

[](#installation)

```
composer require the-jj/spl-collections-sort

```

###  Health Score

29

—

LowBetter than 63% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

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

Total

4

Last Release

3079d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0374dbafe6edc04fef1a46f9c662846a1eba3424e3ccce51a094516263418bcb?d=identicon)[the-jj](/maintainers/the-jj)

---

Top Contributors

[![the-JJ](https://avatars.githubusercontent.com/u/11035165?v=4)](https://github.com/the-JJ "the-JJ (14 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/the-jj-spl-collections-sort/health.svg)

```
[![Health](https://phpackages.com/badges/the-jj-spl-collections-sort/health.svg)](https://phpackages.com/packages/the-jj-spl-collections-sort)
```

PHPackages © 2026

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