Skip to content

Sorting

Every collection and map in the library is sequentially ordered, so sorting is supported across all types.

Naming Convention

The sorting API follows two consistent patterns:

PatternBehavior
sorted*Returns a new sorted collection (available on all types)
sort*Sorts in-place (mutable types only)

Within each pattern:

SuffixInput
(none)Natural order of elements
*DescDescending natural order
*BySelector function — extracts a comparable value
*ByDescSelector, descending
*WithComparator function — full control over comparison

Maps have additional variants for key-specific and value-specific sorting:

MethodTarget
sortedByKey / sortByKeySort by key
sortedByValue / sortByValueSort by value
sortedWithKey / sortWithKeyComparator on keys
sortedWithValue / sortWithValueComparator on values
sortedWith / sortWithComparator on MapEntry objects

Collection / List / Set Sorting

Natural order

php
$list = listOf([3, 1, 4, 1, 5]);

$list->sorted(); // [1, 1, 3, 4, 5]
$list->sortedDesc(); // [5, 4, 3, 1, 1]

Selector (sortedBy)

Extract a comparable value from each element:

php
$users = listOf($users);

$users->sortedBy(fn($u) => $u->name); // alphabetical
$users->sortedByDesc(fn($u) => $u->score); // highest score first

Comparator (sortedWith)

Full control over the comparison:

php
$list->sortedWith(fn($a, $b) => strlen($a) <=> strlen($b));

In-place (mutable only)

php
$list = mutableListOf([3, 1, 2]);

$list->sort(); // [1, 2, 3]
$list->sortDesc(); // [3, 2, 1]
$list->sortBy(fn($n) => -$n); // [3, 2, 1]
$list->sortWith(fn($a, $b) => $a <=> $b); // [1, 2, 3]

Other order operations

php
$list->reversed(); // new list in reverse order
$list->shuffled(); // new list in random order

// In-place (mutable only)
$list->reverse();
$list->shuffle();

Map Sorting

Maps don't have sorted() or sortedDesc() (natural order is ambiguous for key-value pairs), but support selector and comparator patterns plus key/value-specific variants.

By key

php
$map = mapOf(['banana' => 2, 'apple' => 5, 'cherry' => 1]);

$map->sortedByKey(); // apple => 5, banana => 2, cherry => 1
$map->sortedByKeyDesc(); // cherry => 1, banana => 2, apple => 5
$map->sortedByKey(fn($k) => strlen($k)); // apple, banana, cherry (by length)

By value

php
$map->sortedByValue(); // cherry => 1, banana => 2, apple => 5
$map->sortedByValueDesc(); // apple => 5, banana => 2, cherry => 1
$map->sortedByValue(fn($v) => -$v); // apple => 5, banana => 2, cherry => 1

By entry (selector on both key and value)

php
$map->sortedBy(fn($v, $k) => $v); // sort by value using generic selector
$map->sortedByDesc(fn($v, $k) => strlen($k)); // sort by key length, descending

Comparators

php
// Compare keys
$map->sortedWithKey(fn($a, $b) => strcmp($a, $b));

// Compare values
$map->sortedWithValue(fn($a, $b) => $a <=> $b);

// Compare MapEntry objects
$map->sortedWith(fn(MapEntry $a, MapEntry $b) => $a->value <=> $b->value);

sortWith() / sortedWith() on Maps

Map comparators receive two MapEntry objects, not four separate $v, $k, $v, $k arguments. This keeps the callback clean — you access $a->key, $a->value, $b->key, $b->value as needed. Use sortedWithKey() or sortedWithValue() when you only need to compare one of the two.

In-place map sorting (mutable only)

php
$map = mutableMapOf(['b' => 2, 'a' => 1]);

$map->sortByKey(); // a => 1, b => 2
$map->sortByValue(); // a => 1, b => 2
$map->sortByKeyDesc(); // b => 2, a => 1
$map->sortBy(fn($v, $k) => $v);
$map->sortWithKey(fn($a, $b) => strcmp($a, $b));
$map->sortWithValue(fn($a, $b) => $a <=> $b);
$map->sortWith(fn(MapEntry $a, MapEntry $b) => $a->key <=> $b->key);

Other order operations

php
$map->reversed(); // new map in reverse order
$map->shuffled(); // new map in random order

// In-place (mutable only)
$map->reverse();
$map->shuffle();

Quick Reference

Collection / List / Set

MethodReturnsDescription
sorted()newNatural ascending order
sortedDesc()newNatural descending order
sortedBy($selector)newBy selector, ascending
sortedByDesc($selector)newBy selector, descending
sortedWith($comparator)newBy comparator
reversed()newReversed order
shuffled()newRandom order
sort()$thisIn-place ascending
sortDesc()$thisIn-place descending
sortBy($selector)$thisIn-place by selector
sortByDesc($selector)$thisIn-place by selector, desc
sortWith($comparator)$thisIn-place by comparator
reverse()$thisIn-place reverse
shuffle()$thisIn-place shuffle

Map

Maps share sortedBy, sortedByDesc, sortedWith, reversed, shuffled (and their in-place variants) with Collection, plus key/value-specific methods:

MethodReturnsDescription
sortedByKey($selector?)newBy key, optional selector
sortedByKeyDesc($selector?)newBy key desc, optional selector
sortedByValue($selector?)newBy value, optional selector
sortedByValueDesc($selector?)newBy value desc, optional selector
sortedWithKey($comparator)newKey comparator
sortedWithValue($comparator)newValue comparator
sortedWith($comparator)newMapEntry comparator
sortByKey($selector?)$thisIn-place by key
sortByKeyDesc($selector?)$thisIn-place by key desc
sortByValue($selector?)$thisIn-place by value
sortByValueDesc($selector?)$thisIn-place by value desc
sortWithKey($comparator)$thisIn-place key comparator
sortWithValue($comparator)$thisIn-place value comparator
sortWith($comparator)$thisIn-place MapEntry comparator