Created
July 29, 2019 16:46
-
-
Save kevinruscoe/61c414f7ce0102856d45d0b0130f9038 to your computer and use it in GitHub Desktop.
Revisions
-
Kevin Ruscoe revised this gist
Jul 29, 2019 . No changes.There are no files selected for viewing
-
Kevin Ruscoe created this gist
Jul 29, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ Collection::macro( 'sortWithPreference', function ($preference = [], $key = 'id') { return $this->sort( function ($a, $b) use ($preference, $key) { $a = array_search(is_array($a) ? $a[$key] : $a->$key, $preference); $b = array_search(is_array($b) ? $b[$key] : $b->$key, $preference); if ($a === false && $b === false) { return 0; } elseif ($a === false) { return 1; } elseif ($b === false) { return -1; } return $a - $b; } ); } );