{"id":2082,"date":"2021-06-30T00:46:39","date_gmt":"2021-06-30T00:46:39","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2082"},"modified":"2025-04-06T05:03:07","modified_gmt":"2025-04-06T05:03:07","slug":"php-usort","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-usort\/","title":{"rendered":"PHP usort"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PHP <code>usort()<\/code> function to sort an array using a user-defined comparison function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-usort-function'>Introduction to the PHP usort() function <a href=\"#introduction-to-the-php-usort-function\" class=\"anchor\" id=\"introduction-to-the-php-usort-function\" title=\"Anchor for Introduction to the PHP usort() function\">#<\/a><\/h2>\n\n\n\n<p>So far, you learned how to sort an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array\/\">array<\/a> using a built-in comparison operator.<\/p>\n\n\n\n<p>For example, when you use the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array-sort\/\">sort()<\/a><\/code> function to sort an array of numbers, PHP uses the built-in <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-comparison-operators\/\">comparison operator<\/a> to compare the numbers.<\/p>\n\n\n\n<p>To specify a custom comparison function for sorting, you use the <code>usort()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">usort(<span class=\"hljs-keyword\">array<\/span> &amp;$array, callable $callback): bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>usort()<\/code> function has two parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$array<\/code> is the input array.<\/li>\n\n\n\n<li><code>$callback<\/code> is the custom comparison function.<\/li>\n<\/ul>\n\n\n\n<p>The <code>usort()<\/code> function returns <code>true<\/code> on success or <code>false<\/code> or failure.<\/p>\n\n\n\n<p>The <code>$callback<\/code> has the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">callback(mixed $x, mixed $y): int<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>$callback<\/code> function has two parameters which are the array elements to compare.<\/p>\n\n\n\n<p>The <code>$callback<\/code> function compares two elements (<code>$x<\/code> and <code>$y<\/code>) and returns an integer value:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>zero (0) means <code>$x<\/code> is equal to <code>$y<\/code>.<\/li>\n\n\n\n<li>a negative number means <code>$x<\/code> is before <code>$y<\/code>.<\/li>\n\n\n\n<li>a positive number means <code>$x<\/code> is after <code>$y<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='sorting-an-array-of-numbers'>Sorting an array of numbers <a href=\"#sorting-an-array-of-numbers\" class=\"anchor\" id=\"sorting-an-array-of-numbers\" title=\"Anchor for Sorting an array of numbers\">#<\/a><\/h2>\n\n\n\n<p>The following example illustrates how to use the <code>usort()<\/code> function to sort an array of numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$numbers = &#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>];\n\nusort($numbers, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($x, $y)<\/span> <\/span>{\n    <span class=\"hljs-keyword\">if<\/span> ($x === $y) {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n    }\n    <span class=\"hljs-keyword\">return<\/span> $x &lt; $y ? <span class=\"hljs-number\">-1<\/span> : <span class=\"hljs-number\">1<\/span>;\n});\n\nprint_r($numbers);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRudW1iZXJzID0gWzIsIDEsIDNdOwoKdXNvcnQoJG51bWJlcnMsIGZ1bmN0aW9uICgkeCwgJHkpIHsKICAgIGlmICgkeCA9PT0gJHkpIHsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIHJldHVybiAkeCA8ICR5ID8gLTEgOiAxOwp9KTsKCnByaW50X3IoJG51bWJlcnMpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">1<\/span>   \n    &#91;<span class=\"hljs-number\">1<\/span>] =&gt; <span class=\"hljs-number\">2<\/span>   \n    &#91;<span class=\"hljs-number\">2<\/span>] =&gt; <span class=\"hljs-number\">3<\/span>   \n)    <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define an array of three numbers 2, 1, and 3.<\/li>\n\n\n\n<li>Second, use the <code>usort()<\/code> function to sort the <code>$numbers<\/code> array. The callback function returns 0 if two numbers are equal, -1 if the first number is less than the second one, and 1 if the first number is greater than the second one.<\/li>\n<\/ul>\n\n\n\n<p>To sort the elements of the array in descending order, you just need to change the logic in the comparison function like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$numbers = &#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>];\n\nusort($numbers, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($x, $y)<\/span> <\/span>{\n    <span class=\"hljs-keyword\">if<\/span> ($x === $y) {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n    }\n    <span class=\"hljs-keyword\">return<\/span> $x &lt; $y ? <span class=\"hljs-number\">1<\/span> : <span class=\"hljs-number\">-1<\/span>;\n});\n\nprint_r($numbers);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRudW1iZXJzID0gWzIsIDEsIDNdOwoKdXNvcnQoJG51bWJlcnMsIGZ1bmN0aW9uICgkeCwgJHkpIHsKICAgIGlmICgkeCA9PT0gJHkpIHsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIHJldHVybiAkeCA8ICR5ID8gMSA6IC0xOwp9KTsKCnByaW50X3IoJG51bWJlcnMpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>If you use PHP 7 or newer, you can use the spaceship operator (<code>&lt;=&gt;<\/code>) to make the code more concise:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$x &lt;=&gt; $y<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The spaceship operator compares two expressions and returns -1, 0, or 1 when <code>$x<\/code> is respectively less than, equal to, or greater than <code>$y<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$numbers = &#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>];\n\nusort($numbers, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($x, $y)<\/span> <\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x &lt;=&gt; $y;\n});\n\nprint_r($numbers);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRudW1iZXJzID0gWzIsIDEsIDNdOwoKdXNvcnQoJG51bWJlcnMsIGZ1bmN0aW9uICgkeCwgJHkpIHsKICAgIHJldHVybiAkeCA8PT4gJHk7Cn0pOwoKcHJpbnRfcigkbnVtYmVycyk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>If the callback is simple, you can use an arrow function like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$numbers = &#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>];\nusort($numbers, fn ($x, $y) =&gt;  $x &lt;=&gt; $y);\n\nprint_r($numbers);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRudW1iZXJzID0gWzIsIDEsIDNdOwp1c29ydCgkbnVtYmVycywgZm4gKCR4LCAkeSkgPT4gICR4IDw9PiAkeSk7CgpwcmludF9yKCRudW1iZXJzKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Note that PHP introduced the arrow functions since PHP 7.4.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sorting-an-array-of-strings-by-length'>Sorting an array of strings by length <a href=\"#sorting-an-array-of-strings-by-length\" class=\"anchor\" id=\"sorting-an-array-of-strings-by-length\" title=\"Anchor for Sorting an array of strings by length\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>usort()<\/code> function to sort an array of names by length:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$names = &#91; <span class=\"hljs-string\">'Alex'<\/span>, <span class=\"hljs-string\">'Peter'<\/span>,  <span class=\"hljs-string\">'John'<\/span> ];\nusort($names, fn($x,$y) =&gt; strlen($x) &lt;=&gt; strlen($y));\n\nvar_dump($names);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRuYW1lcyA9IFsgJ0FsZXgnLCAnUGV0ZXInLCAgJ0pvaG4nIF07CnVzb3J0KCRuYW1lcywgZm4oJHgsJHkpID0-IHN0cmxlbigkeCkgPD0-IHN0cmxlbigkeSkpOwoKdmFyX2R1bXAoJG5hbWVzKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>(<span class=\"hljs-number\">3<\/span>) \n{\n    &#91;<span class=\"hljs-number\">0<\/span>]=&gt;  string(<span class=\"hljs-number\">4<\/span>) <span class=\"hljs-string\">\"Alex\"<\/span>\n    &#91;<span class=\"hljs-number\">1<\/span>]=&gt;  string(<span class=\"hljs-number\">4<\/span>) <span class=\"hljs-string\">\"John\"<\/span>\n    &#91;<span class=\"hljs-number\">2<\/span>]=&gt;  string(<span class=\"hljs-number\">5<\/span>) <span class=\"hljs-string\">\"Peter\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sorting-an-array-of-objects'>Sorting an array of objects <a href=\"#sorting-an-array-of-objects\" class=\"anchor\" id=\"sorting-an-array-of-objects\" title=\"Anchor for Sorting an array of objects\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>usort()<\/code> function to sort an array of <code>Person<\/code> objects by the <code>age<\/code> property.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> $name;\n    <span class=\"hljs-keyword\">public<\/span> $age;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">(string $name, int $age)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;age = $age;\n    }\n}\n\n$group = &#91;\n    <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Bob'<\/span>, <span class=\"hljs-number\">20<\/span>),\n    <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Alex'<\/span>, <span class=\"hljs-number\">25<\/span>),\n    <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Peter'<\/span>, <span class=\"hljs-number\">30<\/span>),\n];\n\nusort($group, fn($x, $y) =&gt; $x-&gt;age &lt;=&gt; $y-&gt;age);\n\nprint_r($group);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIFBlcnNvbgp7CiAgICBwdWJsaWMgJG5hbWU7CiAgICBwdWJsaWMgJGFnZTsKCiAgICBwdWJsaWMgZnVuY3Rpb24gX19jb25zdHJ1Y3Qoc3RyaW5nICRuYW1lLCBpbnQgJGFnZSkKICAgIHsKICAgICAgICAkdGhpcy0-bmFtZSA9ICRuYW1lOwogICAgICAgICR0aGlzLT5hZ2UgPSAkYWdlOwogICAgfQp9CgokZ3JvdXAgPSBbCiAgICBuZXcgUGVyc29uKCdCb2InLCAyMCksCiAgICBuZXcgUGVyc29uKCdBbGV4JywgMjUpLAogICAgbmV3IFBlcnNvbignUGV0ZXInLCAzMCksCl07Cgp1c29ydCgkZ3JvdXAsIGZuKCR4LCAkeSkgPT4gJHgtPmFnZSA8PT4gJHktPmFnZSk7CgpwcmludF9yKCRncm91cCk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; Person Object\n        (\n            &#91;name] =&gt; Bob\n            &#91;age] =&gt; <span class=\"hljs-number\">20<\/span>\n        )\n    &#91;<span class=\"hljs-number\">1<\/span>] =&gt; Person Object\n        (\n            &#91;name] =&gt; Alex\n            &#91;age] =&gt; <span class=\"hljs-number\">25<\/span>\n        )\n    &#91;<span class=\"hljs-number\">2<\/span>] =&gt; Person Object\n        (\n            &#91;name] =&gt; Peter\n            &#91;age] =&gt; <span class=\"hljs-number\">30<\/span>\n        )\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a <code>Person<\/code> class that has two properties: <code>name<\/code> and <code>age<\/code>.<\/li>\n\n\n\n<li>Second, define the <code>$group<\/code> array that holds the <code>Person<\/code> objects.<\/li>\n\n\n\n<li>Third, use the <code>usort()<\/code> function to sort the <code>Person<\/code> objects of the <code>$group<\/code> array. The <code>usort()<\/code> function uses a comparison function that compares the age of two <code>Person<\/code> objects.<\/li>\n<\/ul>\n\n\n\n<p>If you want to sort the <code>Person<\/code> objects by name, you can compare the <code>$name<\/code> in the comparison like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">usort($group, fn($x, $y) =&gt; $x-&gt;name &lt;=&gt; $y-&gt;name);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='using-a-static-method-as-a-callback'>Using a static method as a callback <a href=\"#using-a-static-method-as-a-callback\" class=\"anchor\" id=\"using-a-static-method-as-a-callback\" title=\"Anchor for Using a static method as a callback\">#<\/a><\/h2>\n\n\n\n<p>The following example uses a <a href=\"https:\/\/phptutorial.net\/php-oop\/php-static-methods\/\">static method<\/a> of class as a callback for the <code>usort()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> $name;\n\n    <span class=\"hljs-keyword\">public<\/span> $age;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">(string $name, int $age)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;age = $age;\n    }\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">PersonComparer<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">compare<\/span><span class=\"hljs-params\">(Person $x, Person $y)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> $x-&gt;age &lt;=&gt; $y-&gt;age;\n    }\n}\n\n$group = &#91;\n    <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Bob'<\/span>, <span class=\"hljs-number\">20<\/span>),\n    <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Alex'<\/span>, <span class=\"hljs-number\">25<\/span>),\n    <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Peter'<\/span>, <span class=\"hljs-number\">30<\/span>),\n];\n\nusort($group, &#91;<span class=\"hljs-string\">'PersonComparer'<\/span>, <span class=\"hljs-string\">'compare'<\/span>]);\n\nprint_r($group);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIFBlcnNvbgp7CiAgICBwdWJsaWMgJG5hbWU7CgogICAgcHVibGljICRhZ2U7CgogICAgcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KHN0cmluZyAkbmFtZSwgaW50ICRhZ2UpCiAgICB7CiAgICAgICAgJHRoaXMtPm5hbWUgPSAkbmFtZTsKICAgICAgICAkdGhpcy0-YWdlID0gJGFnZTsKICAgIH0KfQoKY2xhc3MgUGVyc29uQ29tcGFyZXIKewogICAgcHVibGljIHN0YXRpYyBmdW5jdGlvbiBjb21wYXJlKFBlcnNvbiAkeCwgUGVyc29uICR5KQogICAgewogICAgICAgIHJldHVybiAkeC0-YWdlIDw9PiAkeS0-YWdlOwogICAgfQp9CgokZ3JvdXAgPSBbCiAgICBuZXcgUGVyc29uKCdCb2InLCAyMCksCiAgICBuZXcgUGVyc29uKCdBbGV4JywgMjUpLAogICAgbmV3IFBlcnNvbignUGV0ZXInLCAzMCksCl07Cgp1c29ydCgkZ3JvdXAsIFsnUGVyc29uQ29tcGFyZXInLCAnY29tcGFyZSddKTsKCnByaW50X3IoJGdyb3VwKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, we define the <code>PersonComparer<\/code> class that contains the <code>compare()<\/code> static method.<\/p>\n\n\n\n<p>The <code>compare()<\/code> static method compares two <code>Person<\/code> objects by age using the spaceship operator.<\/p>\n\n\n\n<p>To use the <code>compare()<\/code> static method of the <code>PersonComparer<\/code> class as the callback the <code>usort()<\/code> function, you pass an array that contains two elements:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">usort($group, &#91;<span class=\"hljs-string\">'PersonComparer'<\/span>, <span class=\"hljs-string\">'compare'<\/span>]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The first element is the class name and the second one is the static method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the PHP <code>usort()<\/code> function to sort an array using a user-defined comparison function.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2082\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-usort\/\"\n\t\t\t\tdata-post-title=\"PHP usort\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2082\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-usort\/\"\n\t\t\t\tdata-post-title=\"PHP usort\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PHP usort() function to sort an array using a user-defined comparison function.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":59,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2082","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2082"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2082\/revisions"}],"predecessor-version":[{"id":3098,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2082\/revisions\/3098"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}