How to Use PHP usort() for Custom Array Sorting

This article explains the PHP usort() function, its syntax, how to write comparison callbacks, and provides practical examples for sorting numeric arrays, associative arrays of objects, and handling common pitfalls such as stability and in‑place modification.

php Courses
php Courses
php Courses
How to Use PHP usort() for Custom Array Sorting

PHP is a widely used programming language that provides many built‑in functions for developers, among which usort() is used to sort an array according to a user‑defined comparison function.

The syntax of usort() is:

usort(array $array, callable $cmp_function): bool

Here $array is the array to be sorted and $cmp_function is a callable that determines the ordering. The callback must return 0, -1, or 1 to indicate equality, less‑than, or greater‑than respectively.

Comparison function

The comparison function must be callable. It can be an anonymous function or a named function. For example:

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

In this function, $a and $b are the two elements being compared. Returning 0 means they are equal, -1 means $a is smaller, and 1 means $a is larger.

Usage example

Sorting a simple numeric array:

$array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
usort($array, "cmp");

After execution, $array becomes [1, 1, 2, 3, 3, 4, 5, 5, 6, 9].

Sorting an array of associative arrays (e.g., students) by age:

$students = [
    ["name" => "Alice",   "age" => 18],
    ["name" => "Bob",     "age" => 20],
    ["name" => "Charlie", "age" => 19],
];
function cmp_age($a, $b) {
    if ($a["age"] == $b["age"]) {
        return 0;
    }
    return ($a["age"] < $b["age"]) ? -1 : 1;
}

usort($students, "cmp_age");

The resulting order is Alice (18), Charlie (19), Bob (20).

Important notes

The comparison function must be callable.

It must return only 0, -1, or 1. usort() modifies the original array in place, so be aware of side effects.

If the array contains equal elements, usort() does not guarantee their relative order.

Conclusion

The usort() function is a common and powerful tool in PHP for custom sorting of arrays. By defining an appropriate comparison callback and passing it to usort(), developers can sort numbers, strings, objects, or any complex structures while being mindful of the function’s constraints.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

array sortingusortcomparison-function
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.