Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
Created September 22, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smailliwcs/230743dc79cd4283fd4eabc89aa146cf to your computer and use it in GitHub Desktop.
Save smailliwcs/230743dc79cd4283fd4eabc89aa146cf to your computer and use it in GitHub Desktop.
Underscore.php mixin: sort
Array
(
[0] => 5
[1] => 1
[2] => 3
[3] => 0
[4] => 6
[5] => 8
[6] => 4
[7] => 9
[8] => 7
[9] => 2
)
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
)
Array
(
[0] => 9
[1] => 8
[2] => 7
[3] => 6
[4] => 5
[5] => 4
[6] => 3
[7] => 2
[8] => 1
[9] => 0
)
Array
(
[0] => 5
[1] => 1
[2] => 3
[3] => 0
[4] => 6
[5] => 8
[6] => 4
[7] => 9
[8] => 7
[9] => 2
)
<?php
__()->mixin([
"sort" => function ($items, $ascending = true) {
if ($ascending) {
sort($items);
} else {
rsort($items);
}
return $items;
}
]);
$numbers = __()->chain()->range(10)->shuffle()->value();
print_r($numbers);
print_r(__($numbers)->sort());
print_r(__($numbers)->sort(false));
print_r($numbers);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment