usort
laravel/framework
Supported Versions: PHP 4, PHP 5, PHP 7, PHP 8
Sort an array by values using a user-defined comparison function
usort
(array &
$array
, callable
$callback
):
bool
{
$items
=
$this
->
items
;
usort
(
$items
, function (
$a
,
$b
) use (
$comparisons
) {
foreach (
$comparisons
as
$comparison
) {
$comparison
=
Arr
::
wrap
(
$comparison
);
$prop
=
$comparison
[
0
];
$ascending
=
Arr
::
get
(
$comparison
,
1
,
true
) ===
true
||
Arr
::
get
(
$comparison
,
1
,
true
) ===
'asc'
;
if (!
is_string
(
$prop
) &&
is_callable
(
$prop
)) {
$result
=
$prop
(
$a
,
$b
);
} else {
$values
= [
data_get
(
$a
,
$prop
),
data_get
(
$b
,
$prop
)];
if (!
$ascending
) {
$values
=
array_reverse
(
$values
);
}
$result
=
$values
[
0
] <=>
$values
[
1
];
}
if (
$result
===
0
) {
continue;
}
return
$result
;
}
});
return new static(
$items
);
}
src/Illuminate/Collections/Collection.php