sort() public method

Sort through each item with a callback.
public sort ( callable $callback = null ) : static
$callback callable
return static
Example #1
0
 /**
  * @param string $level
  *
  * @return \Illuminate\Support\Collection
  */
 public function get($level = '*')
 {
     $this->messages->sort(function ($a, $b) {
         return $b['sort'] - $a['sort'];
     });
     return $level !== '*' ? $this->filterNotifications($level) : $this->messages;
 }
 /**
  * Bootstrap the application services.
  */
 public function boot()
 {
     parent::boot();
     $this->publishMigrations();
     $app = $this->app;
     if ($app->bound('form')) {
         $app->form->macro('selectCountry', function ($name, $selected = null, $options = []) use($app) {
             $countries = Cache::rememberForever('brianfaust.countries.select.name.cca2', function () {
                 $records = Country::get(['name', 'cca2']);
                 $countries = new Collection();
                 $records->map(function ($item) use(&$countries) {
                     $countries[$item['cca2']] = $item['name']['official'];
                 });
                 return $countries->sort();
             });
             return $app->form->select($name, $countries, $selected, $options);
         });
         $app->form->macro('selectCountryWithId', function ($name, $selected = null, $options = []) use($app) {
             $countries = Cache::rememberForever('brianfaust.countries.select.id.cca2', function () {
                 $records = Country::get(['name', 'id']);
                 $countries = new Collection();
                 $records->map(function ($item) use(&$countries) {
                     $countries[$item['id']] = $item['name']['official'];
                 });
                 return $countries->sort();
             });
             return $app->form->select($name, $countries, $selected, $options);
         });
     }
 }
 /**
  * Show the application dashboard.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     //grab this user's bookmarks and any public bookmarks
     $bookmarks = Bookmark::where('user_id', '=', Auth::user()->id)->orWhere('private', '=', false)->orderBy('created_at', 'DESC')->orderBy('name', 'ASC')->get();
     //grab only the tags that this user has permission to see
     $tags = new Collection();
     foreach ($bookmarks as $bookmark) {
         $tags_as_string = "";
         $these_tags = new Collection();
         foreach ($bookmark->tags()->get() as $tag) {
             $tags->push($tag);
             $these_tags->push($tag->name);
         }
         $tags_as_string = rtrim(implode(', ', $these_tags->sort()->toArray()), ',');
         //$tags_as_string = $tags_as_string,',');
         $bookmark['tags'] = $bookmark['tags']->sortBy('name');
         $bookmark['tags_as_string'] = $tags_as_string;
     }
     $tags = $tags->sortBy('name')->unique('name');
     //for stats
     $untagged_count = $bookmarks->filter(function ($item) {
         return $item->tags()->count() == 0;
     })->count();
     //total count
     $all_bm_count = $bookmarks->count();
     //public only count
     $public_bm_count = Bookmark::where('private', '=', false)->count();
     //private only count
     $private_bm_count = Bookmark::where('user_id', '=', Auth::user()->id)->where('private', '=', true)->count();
     //for any actions, lets grab the messages
     $message = \Session::get('message') ? \Session::get('message') : array();
     //render with all of the necassary data
     return view('dashboard')->with(array('bookmarks' => $bookmarks, 'tags' => $tags, 'stats' => array('untagged_count' => $untagged_count, 'all_bm_count' => $all_bm_count, 'public_bm_count' => $public_bm_count, 'private_bm_count' => $private_bm_count), 'message' => $message));
 }
 /**
  * Show a list of all available handlers.
  *
  * @param  Collection|SignatureHandler[] $handlers
  * @return Response
  */
 protected function displayListOfAllCommands(Collection $handlers) : Response
 {
     $attachmentFields = $handlers->sort(function (SignatureHandler $handlerA, SignatureHandler $handlerB) {
         return strcmp($handlerA->getFullCommand(), $handlerB->getFullCommand());
     })->map(function (SignatureHandler $handler) {
         return AttachmentField::create($handler->getFullCommand(), $handler->getDescription());
     })->all();
     return $this->respondToSlack('Available commands:')->withAttachment(Attachment::create()->setColor('good')->setFields($attachmentFields));
 }
Example #5
0
 /**
  * @throws \InvalidArgumentException
  *
  * @return Collection|PotentialOffer[]
  */
 private function collect() : Collection
 {
     /*
      * Create buckets for each offer, keeping duplicate offers.
      * Assign each product to the first applicable bucket with spaces left.
      * Filter the buckets to those that are full.
      */
     return $this->components->sort(function (OfferComponent $a, OfferComponent $b) {
         $priceA = $a->product()->price()->asFloat();
         $priceB = $b->product()->price()->asFloat();
         return $priceA <=> $priceB;
     })->reduce(function (Collection $potentials, OfferComponent $component) {
         $potentials->first(function (PotentialOffer $potential) use($component) {
             return $potential->takeComponent($component);
         });
         return $potentials;
     }, $this->potentials())->filter(function (PotentialOffer $offer) {
         return $offer->requirementsAreMet();
     })->sort(function (PotentialOffer $offer) {
         return $offer->linePrice()->negative()->amount();
     });
 }
Example #6
0
 public static function order(Collection $items)
 {
     return $items->sort(function ($item1, $item2) {
         $item1 = self::getItem($item1);
         $item2 = self::getItem($item2);
         if ($item1['weight'] > $item2['weight']) {
             return 1;
         }
         if ($item1['weight'] < $item2['weight']) {
             return -1;
         }
         return 0;
     });
 }
 public function transformArray(array $beneficiaries)
 {
     $beneficiariesByCampaign = [];
     $modelCollection = new Collection($beneficiaries);
     $modelCollection->sort(function ($a, $b) {
         return strcasecmp($a->name, $b->name);
     })->each(function (Beneficiary $o) use(&$beneficiariesByCampaign) {
         $result = ['uid' => $o->uid, 'name' => $o->name, 'logo' => $o->logo ? cdn_asset($o->logo, 'logos/beneficiaries/') : null, 'url' => $o->url, 'description' => $o->description, 'show_location' => $this->bool($o->show_location), 'organization_name' => $o->organization->name, 'ctas' => $o->beneficiaryCallsToAction ? $o->beneficiaryCallsToAction->filter(function ($o) {
             return $o->participating;
         })->map(function ($o) {
             return $o->callToAction->permalink;
         })->all() : null];
         $o->campaigns->each(function ($c) use(&$o, &$beneficiariesByCampaign, $result) {
             if (!isset($beneficiariesByCampaign[$c->permalink])) {
                 $beneficiariesByCampaign[$c->permalink] = [];
             }
             $beneficiariesByCampaign[$c->permalink][] = $result;
         });
     });
     ksort($beneficiariesByCampaign);
     return $beneficiariesByCampaign;
 }
 /**
  * Sort through each item with a callback.
  *
  * @param  callable|null $callback
  * @return static
  */
 public function sort(callable $callback = null)
 {
     return parent::sort($callback ?: function (Addon $a, Addon $b) {
         if ($a->getSlug() == $b->getSlug()) {
             return 0;
         }
         return $a->getSlug() < $b->getSlug() ? -1 : 1;
     });
 }
Example #9
0
 /**
  * @param \Illuminate\Support\Collection $tasks
  *
  * @return \Illuminate\Support\Collection
  */
 private function getFilteredOrderedTasks(Collection $tasks)
 {
     return $tasks->sort(function ($a, $b) {
         $aProjectId = array_get($a, 'relationships.project.data.id');
         $bProjectId = array_get($b, 'relationships.project.data.id');
         if ($aProjectId === $bProjectId) {
             if ($a['id'] === $b['id']) {
                 return 0;
             }
             return $a['id'] < $b['id'] ? -1 : 1;
         }
         return $aProjectId < $bProjectId ? -1 : 1;
     });
 }