PHPackages                             jast99/sorted-linked-list - 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. jast99/sorted-linked-list

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

jast99/sorted-linked-list
=========================

SortedLinkedList

v1.0.0(3mo ago)01MITPHPPHP ^8.2

Since Mar 20Pushed 3mo agoCompare

[ Source](https://github.com/JaSt99/sorted-linked-list)[ Packagist](https://packagist.org/packages/jast99/sorted-linked-list)[ RSS](/packages/jast99-sorted-linked-list/feed)WikiDiscussions main Synced 3w ago

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

SortedLinkedList
================

[](#sortedlinkedlist)

A PHP library providing a sorted linked list that keeps values in order. It can hold `int` or `string` values, but not both at the same time.

Requires PHP 8.2+.

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

[](#installation)

```
composer require jast99/sorted-linked-list
```

Usage
-----

[](#usage)

### Creating a list

[](#creating-a-list)

```
use JaSt99\SortedLinkedList\SortedLinkedList;

$integers = SortedLinkedList::ofIntegers();
$strings = SortedLinkedList::ofStrings();
```

### Inserting values

[](#inserting-values)

Values are automatically inserted in sorted order:

```
$list = SortedLinkedList::ofIntegers();
$list->insert(5);
$list->insert(1);
$list->insert(3);

$list->toArray(); // [1, 3, 5]
```

### Removing values

[](#removing-values)

Returns `true` if the value was found and removed, `false` otherwise. If duplicates exist, only the first occurrence is removed.

```
$list->remove(3);  // true
$list->remove(99); // false
```

### Checking for values

[](#checking-for-values)

```
$list->contains(5);  // true
$list->contains(99); // false
```

### Accessing first and last element

[](#accessing-first-and-last-element)

```
$list->first(); // smallest value
$list->last();  // largest value
```

Both methods throw `EmptyListException` when the list is empty.

### Count and emptiness

[](#count-and-emptiness)

```
$list->count();   // number of elements
$list->isEmpty(); // true if empty
count($list);     // works too (Countable interface)
```

### Iteration

[](#iteration)

The list implements `IteratorAggregate`, so you can use `foreach`:

```
foreach ($list as $value) {
    echo $value . PHP_EOL;
}
```

### Converting to array and string

[](#converting-to-array-and-string)

```
$list->toArray();        // [1, 3, 5]
$list->toString();       // '1, 3, 5'
$list->toString(';');    // '1;3;5'
$list->toString(' | ');  // '1 | 3 | 5'
```

Custom comparator
-----------------

[](#custom-comparator)

By default, values are sorted using PHP's native `` operator. You can provide a custom comparator:

```
// Case-insensitive string sorting
$list = SortedLinkedList::ofStrings(strcasecmp(...));
$list->insert('Banana');
$list->insert('apple');
$list->toArray(); // ['apple', 'Banana']

// Reverse integer sorting
$list = SortedLinkedList::ofIntegers(fn(int $a, int $b): int => $b  $a);
$list->insert(1);
$list->insert(3);
$list->toArray(); // [3, 1]
```

Type safety
-----------

[](#type-safety)

The list enforces type consistency both at static analysis level (PHPStan generics) and at runtime:

```
$list = SortedLinkedList::ofIntegers();
$list->insert('foo'); // throws ListTypeMismatchException
```

Development
-----------

[](#development)

```
# Run tests
vendor/bin/phpunit

# Run static analysis
vendor/bin/phpstan analyse

# Check coding standard
vendor/bin/phpcs
```

License
-------

[](#license)

MIT

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance82

Actively maintained with recent releases

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

95d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/433a4789804fddc2450f53bc4311a0a90b1e901084f7ab70e9a2fb091211cb51?d=identicon)[JaSt99](/maintainers/JaSt99)

---

Top Contributors

[![JaSt99](https://avatars.githubusercontent.com/u/33695273?v=4)](https://github.com/JaSt99 "JaSt99 (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jast99-sorted-linked-list/health.svg)

```
[![Health](https://phpackages.com/badges/jast99-sorted-linked-list/health.svg)](https://phpackages.com/packages/jast99-sorted-linked-list)
```

###  Alternatives

[psr7-sessions/storageless

Storageless PSR-7 Session support

650396.3k8](/packages/psr7-sessions-storageless)[roots/wp-stage-switcher

WordPress plugin that allows you to switch between different environments from the admin bar

374458.3k3](/packages/roots-wp-stage-switcher)[fg/parkour

A collection of utilities to manipulate arrays.

14490.6k3](/packages/fg-parkour)[mnastalski/baselinker-php

Base.com (formerly BaseLinker) PHP library

1635.7k](/packages/mnastalski-baselinker-php)[beholdr/filament-trilist

Filament plugin that adds components for working with tree data: treeselect and treeview

1124.6k](/packages/beholdr-filament-trilist)

PHPackages © 2026

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