-
app/Helpers/CollectionHelper.php
Open in GitHubuse Illuminate\Support\Collection; class CollectionHelper { public static function sortByCollator($collect, $callback, $options = \Collator::SORT_STRING, $descending = false) { $results = []; $callback = static::valueRetriever($callback); foreach ($collect->all() as $key => $value) { $results[$key] = $callback($value, $key); } static::getCollator()->asort($results, $options); if ($descending) { $results = array_reverse($results); } foreach (array_keys($results) as $key) { $results[$key] = $collect->get($key); } return new Collection($results); } // }
-
app/Providers/MacroServiceProvider.php
Open in GitHubuse App\Helpers\CollectionHelper; use Illuminate\Support\Collection; use Illuminate\Support\ServiceProvider; class MacroServiceProvider extends ServiceProvider { public function boot() { if (! Collection::hasMacro('sortByCollator')) { Collection::macro('sortByCollator', function ($callback, $options = \Collator::SORT_STRING, $descending = false) { $collect = $this; return CollectionHelper::sortByCollator($collect, $callback, $options, $descending); }); } // } }
-
app/Http/Controllers/Settings/GendersController.php
Open in GitHubclass GendersController extends Controller { public function index() { $gendersData = collect([]); $genders = auth()->user()->account->genders; foreach ($genders as $gender) { $gendersData->push($this->formatData($gender)); } return CollectionHelper::sortByCollator($gendersData, 'name'); } // }