PHPackages                             antikirra/array-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. antikirra/array-sort

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

antikirra/array-sort
====================

2.1.0(1y ago)07MITPHPPHP ^5.6 || ^7.0 || ^8.0

Since May 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/antikirra/array-sort)[ Packagist](https://packagist.org/packages/antikirra/array-sort)[ RSS](/packages/antikirra-array-sort/feed)WikiDiscussions main Synced 1mo ago

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

Human-readable array sorter for PHP
===================================

[](#human-readable-array-sorter-for-php)

[![Packagist Dependency Version](https://camo.githubusercontent.com/ea0eae1e8038b368530509ea67067c4fbe78ab8f9a74291d9824be8a055699ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f616e74696b697272612f61727261792d736f72742f706870)](https://camo.githubusercontent.com/ea0eae1e8038b368530509ea67067c4fbe78ab8f9a74291d9824be8a055699ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f616e74696b697272612f61727261792d736f72742f706870)[![Packagist Version](https://camo.githubusercontent.com/7d261c74f2c38bcb1cdb9d4e8097a92d58bbad7b647662122e8da9eb7c5d7ac5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e74696b697272612f61727261792d736f7274)](https://camo.githubusercontent.com/7d261c74f2c38bcb1cdb9d4e8097a92d58bbad7b647662122e8da9eb7c5d7ac5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e74696b697272612f61727261792d736f7274)

Install
-------

[](#install)

```
composer require antikirra/array-sort:^2
```

Sorting Order Options
---------------------

[](#sorting-order-options)

This library provides three primary sorting methods for arrays: by key, by value, and by a custom user-defined function. To apply your preferred sorting method, simply use the appropriate static method from the base facade - `::byKey()`, `::byValue()` or `::byFunction(callable)`.

```
Sorter::byKey()->sort($array); // {"a":8,"b":3,"c":5,"d":11}
Sorter::byValue()->sort($array); // {"b":3,"c":5,"a":8,"d":11}
```

Default Key Association
-----------------------

[](#default-key-association)

By default, array values are sorted while preserving their original keys. This behavior aligns with typical expectations when using sorting functions. If you prefer to reindex keys without making additional `array_values()` calls, simply use the `->resetKeys()` method.

Custom Sorting for Multidimensional Arrays
------------------------------------------

[](#custom-sorting-for-multidimensional-arrays)

Custom function-based sorting is available for those rare cases involving multidimensional arrays. Unfortunately, the standard interface for interacting with such arrays is quite limiting and lacks elegance, making it challenging to implement a facade that remains backward-compatible with earlier PHP versions. However, you can use the `::asc` and `::desc` helpers to specify elements at any desired depth within nested arrays.

**Be extremely cautious!** Changing the order of parameters can reverse the sorting result. Always ensure that the argument order in the outer function matches that in the inner function to maintain the expected sorting behavior.

```
Sorter::byFunction(function ($a, $b) {
    return Sorter::desc($a['age'], $b['age']);
})->sort($users);
```

Default Sorting Behavior
------------------------

[](#default-sorting-behavior)

When sorting by key or by value, you can specify the sorting order: either ascending or descending - `->asc()` or `->desc()`. By default, sorting is performed in ascending order. Explicitly calling the `->asc()` method is optional and recommended only when it improves code readability.

Performance and Array Integrity
-------------------------------

[](#performance-and-array-integrity)

Native PHP array-sorting functions are designed for maximum performance and minimal memory usage. However, in everyday use, you might often need to keep the original array intact. You can use the `->sortCopy($array)` method to create a copy of the original array and then apply sorting. Alternatively, the `->sort(&$array)` method performs sorting directly on the array as expected.

Basic usage
-----------

[](#basic-usage)

```
