PHPackages                             yarri/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. yarri/array-sort

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

yarri/array-sort
================

The definitive function for sorting arrays in PHP

v0.1(2y ago)01.6kMITPHPPHP &gt;=7.0.0CI passing

Since Jan 23Pushed 2mo ago1 watchersCompare

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

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

ArraySort
=========

[](#arraysort)

[![Tests](https://github.com/yarri/ArraySort/actions/workflows/tests.yml/badge.svg)](https://github.com/yarri/ArraySort/actions/workflows/tests.yml)

The definitive function for sorting arrays in PHP.

Function signatures
-------------------

[](#function-signatures)

```
array_sort(array $array): array
array_sort(array $array, array $options = []): array
array_sort(array $array, int $flags = SORT_LOCALE_STRING): array
array_sort(array $array, int $flags = SORT_LOCALE_STRING, array $options = []): array
array_sort(array $array, int $flags = SORT_LOCALE_STRING, callable $callback = null, array $options = []): array
array_sort(array $array, callback $callback = null, array $options = []): array

```

#### Parameters

[](#parameters)

**array**

The input array.

**flags**

The parameter flags may be used to modify the sorting behavior using these values:

- SORT\_REGULAR - compare items normally; the details are described in the comparison operators section
- SORT\_NUMERIC - compare items numerically
- SORT\_STRING - compare items as strings
- SORT\_LOCALE\_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
- SORT\_NATURAL - compare items as strings using "natural ordering" like natsort()
- SORT\_FLAG\_CASE - can be combined (bitwise OR) with SORT\_STRING or SORT\_NATURAL to sort strings case-insensitively

**callback**

The parameter callback is a function to convert an array item to its string or numeric form. If it is set to NULL the default function is used:

```
$callback = function($item){ return (string)$item; };

```

**options**

The default options are:

```
Array(
  "sort_keys" => false,
  "reverse" => false,
  "preserve_keys" => false,
)

```

#### Return value

[](#return-value)

The function array\_sort returns the sorted array.

Usage
-----

[](#usage)

```
$fruits = ["d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"];

array_sort($fruits); // ["apple", "banana", "lemon", "orange"]
array_sort($fruits,["reverse" => true]); // ["orange", "lemon", "banana", "apple"]

array_sort($fruits,["preserve_keys" => true]); // ["c" => "apple", "b" => "banana", "d" => "lemon", "a" => "orange"]
array_sort($fruits,["preserve_keys" => true, "reverse" => true]); // ["a" => "orange", "d" => "lemon", "b" => "banana", "c" => "apple"]

// === SORTING KEYS ===

array_sort($fruits,["sort_keys" => true]); // ["a" => "orange", "b" => "banana", "c" => "apple", "d" => "lemon"]
array_sort($fruits,["sort_keys" => true, "reverse" => true]); // ["d" => "lemon", "c" => "apple", "b" => "banana", "a" => "orange"]

// === NUMERIC SORTING ===

$numbers = ["d" => 200, "a" => 100, "b" => 20, "c" => 10];

// not yet :)
array_sort($numbers); // [10, 100, 20, 200];

array_sort($numbers,SORT_NUMERIC); // [10, 20, 100, 200]
array_sort($numbers,SORT_NUMERIC,["reverse" => true]); // [200, 100, 20, 10]

array_sort($numbers,SORT_NUMERIC,["preserve_keys" => true]); // ["c" => 10, "b" => 20, "a" => 100, "d" => 200]

// === SORTING OF STRUCTURES ===

$books = [
  [
    "author" => "Verne, Jules",
    "title" => "Around the World in Eighty Days",
  ],
  [
    "author" => "Tolstoy, Leo",
    "title" => "War and Peace",
  ],
  [
    "author" => "King, Stephen",
    "title" => "The X-Files",
  ],
  [
    "author" => "Toole, John Kennedy",
    "title" => "A Confederacy of Dunces",
  ],
];

// sorting by the author
array_sort($books, function($book){ return $book["author"]; });
// [
//   [
//     "author" => "King, Stephen",
//     "title" => "The X-Files",
//   ],
//   [
//     "author" => "Tolstoy, Leo",
//     "title" => "War and Peace",
//   ],
//   [
//     "author" => "Toole, John Kennedy",
//     "title" => "A Confederacy of Dunces",
//   ],
//   [
//     "author" => "Verne, Jules",
//     "title" => "Around the World in Eighty Days",
//   ],
// ]

// sorting by the title without article
array_sort($books, function($book){ return preg_replace("/^(an?|the) /i","",$book["title"]); });
// [
//   [
//     "author" => "Verne, Jules",
//     "title" => "Around the World in Eighty Days",
//   ],
//   [
//     "author" => "Toole, John Kennedy",
//     "title" => "A Confederacy of Dunces",
//   ],
//   [
//     "author" => "Tolstoy, Leo",
//     "title" => "War and Peace",
//   ],
//   [
//     "author" => "King, Stephen",
//     "title" => "The X-Files",
//   ],
// ]

```

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

[](#installation)

Just use the Composer:

```
composer require yarri/array-sort

```

License
-------

[](#license)

ArraySort is free software distributed [under the terms of the MIT license](http://www.opensource.org/licenses/mit-license)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance58

Moderate activity, may be stable

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 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

846d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6304dffbd91d7a978f98632b0e4e30d662dcdb691daadb1388a58984e98faf5c?d=identicon)[yarri](/maintainers/yarri)

---

Top Contributors

[![yarri](https://avatars.githubusercontent.com/u/974278?v=4)](https://github.com/yarri "yarri (23 commits)")

### Embed Badge

![Health badge](/badges/yarri-array-sort/health.svg)

```
[![Health](https://phpackages.com/badges/yarri-array-sort/health.svg)](https://phpackages.com/packages/yarri-array-sort)
```

###  Alternatives

[reefki/laravel-device-detector

Laravel wrapper for Matomo's Universal Device Detection library.

2852.6k](/packages/reefki-laravel-device-detector)

PHPackages © 2026

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