sortBy() public method

Sort the collection using the given callback.
public sortBy ( callable | string $callback, integer $options = SORT_REGULAR, boolean $descending = false ) : static
$callback callable | string
$options integer
$descending boolean
return static
Example #1
0
 /**
  * @return Collection
  */
 public function getCategories() : Collection
 {
     $set = $this->categories->sortBy(function (CategoryModel $category) {
         return $category->spent;
     });
     return $set;
 }
Example #2
0
 /**
  * @return Collection
  */
 public function getExpenses()
 {
     $set = $this->expenses->sortBy(function (stdClass $object) {
         return $object->amount;
     });
     return $set;
 }
 /**
  * Perform ordering
  *
  * @return mixed
  */
 public function ordering()
 {
     foreach ($this->request->getOrder() as $key => $reverse) {
         $this->collection = $this->collection->sortBy(function ($row) use($key) {
             return $row[$key];
         }, SORT_NATURAL, $reverse);
     }
 }
Example #4
0
 /**
  * @return Collection
  */
 public function getBills()
 {
     $set = $this->bills->sortBy(function (BillLine $bill) {
         $active = intval($bill->getBill()->active) == 0 ? 1 : 0;
         $name = $bill->getBill()->name;
         return $active . $name;
     });
     return $set;
 }
 /**
  * Adds a OrderBy to the query
  *
  * @param  string
  * @param  string
  * @return $this
  */
 public function orderBy($column, $direction = 'asc')
 {
     if ($column != null) {
         if ($direction === 'asc') {
             $this->collection = $this->collection->sortBy($column);
         } elseif ($direction === 'desc') {
             $this->collection = $this->collection->sortByDesc($column);
         }
     }
     return $this;
 }
 /**
  * @inheritdoc
  */
 public function ordering()
 {
     foreach ($this->request->orderableColumns() as $orderable) {
         $column = $this->getColumnName($orderable['column']);
         $this->collection = $this->collection->sortBy(function ($row) use($column) {
             return $row[$column];
         });
         if ($orderable['direction'] == 'desc') {
             $this->collection = $this->collection->reverse();
         }
     }
 }
 private function doInternalOrder()
 {
     if (is_null($this->orderColumn)) {
         return;
     }
     $column = $this->orderColumn[0];
     $stripOrder = $this->options['stripOrder'];
     $self = $this;
     $this->workingCollection = $this->workingCollection->sortBy(function ($row) use($column, $stripOrder, $self) {
         if ($self->getAliasMapping()) {
             $column = $self->getNameByIndex($column);
         }
         if ($stripOrder) {
             if (is_callable($stripOrder)) {
                 return $stripOrder($row, $column);
             } else {
                 return strip_tags($row[$column]);
             }
         } else {
             return $row[$column];
         }
     }, SORT_NATURAL);
     if ($this->orderDirection == BaseEngine::ORDER_DESC) {
         $this->workingCollection = $this->workingCollection->reverse();
     }
 }
 /**
  * @return $this
  */
 protected function sortWidgets()
 {
     $this->registeredWidgets = $this->registeredWidgets->sortBy(function (WidgetCollectionItem $widget) {
         return $widget->getPosition();
     });
     return $this;
 }
Example #9
0
 /**
  * @return Collection
  */
 public function getExtensions()
 {
     $extensionsDir = $this->getExtensionsDir();
     $dirs = array_diff(scandir($extensionsDir), ['.', '..']);
     $extensions = new Collection();
     $installed = json_decode(file_get_contents(public_path('vendor/composer/installed.json')), true);
     foreach ($dirs as $dir) {
         if (file_exists($manifest = $extensionsDir . '/' . $dir . '/composer.json')) {
             $extension = new Extension($extensionsDir . '/' . $dir, json_decode(file_get_contents($manifest), true));
             if (empty($extension->name)) {
                 continue;
             }
             foreach ($installed as $package) {
                 if ($package['name'] === $extension->name) {
                     $extension->setInstalled(true);
                     $extension->setVersion($package['version']);
                     $extension->setEnabled($this->isEnabled($dir));
                 }
             }
             $extensions->put($dir, $extension);
         }
     }
     return $extensions->sortBy(function ($extension, $name) {
         return $extension->composerJsonAttribute('extra.flarum-extension.title');
     });
 }
Example #10
0
 /**
  * Sorts the modules in an order that will suite the natural defaults
  * for their menu presence.
  *
  * @return $this
  */
 protected function sortModules()
 {
     $this->modules = $this->modules->sortBy(function (ModuleInterface $module) {
         return $module->getName();
     });
     return $this;
 }
 /**
  * @param string $name
  * @param array  $params
  */
 public function run($name, array $params = [])
 {
     $widgets = static::getWidgetsByBlock($name, $params);
     $collection = new Collection($widgets);
     $collection->sortBy(function ($widget) {
         return $widget->getPosition();
     });
     echo view('pages::pages.wysiwyg.block_placeholder', ['widgets' => $collection, 'name' => $name, 'page' => $this->page])->render();
 }
Example #12
0
 /**
  * @inheritdoc
  */
 public function doOrdering()
 {
     if (array_key_exists('order', $this->input) && count($this->input['order']) > 0) {
         for ($i = 0, $c = count($this->input['order']); $i < $c; $i++) {
             $order_col = (int) $this->input['order'][$i]['column'];
             $order_dir = $this->input['order'][$i]['dir'];
             if (!$this->isColumnOrderable($this->input['columns'][$order_col])) {
                 continue;
             }
             $column = $this->getOrderColumnName($order_col);
             $this->collection = $this->collection->sortBy(function ($row) use($column) {
                 return $row[$column];
             });
             if ($order_dir == 'desc') {
                 $this->collection = $this->collection->reverse();
             }
         }
     }
 }
 /**
  * @inheritdoc
  */
 public function ordering()
 {
     foreach ($this->request->orderableColumns() as $orderable) {
         $column = $this->getColumnName($orderable['column']);
         $this->collection = $this->collection->sortBy(function ($row) use($column) {
             $row = Helper::castToArray($row);
             return Arr::get($row, $column);
         });
         if ($orderable['direction'] == 'desc') {
             $this->collection = $this->collection->reverse();
         }
     }
 }
Example #14
0
 /**
  * Show the form for editing the specified resource.
  *
  * @param int $id
  *
  * @return Response
  */
 public function edit($id)
 {
     if (!($user = $this->userRepositoryInterface->getByIdWith($id, 'roles'))) {
         Flash::error(trans('dashboard::dashboard.errors.user.found'));
         return redirect()->route('users.index');
     }
     $currentRoles = $user->getRoles()->lists('name');
     if (empty($currentRoles)) {
         $currentRoles = new Collection(['name' => 'Not Available']);
     }
     $currentRoles->sortBy('name');
     $currentRoles = implode(', ', $currentRoles->toArray());
     $roles = $this->roleRepositoryInterface->getAll()->lists('name', 'slug');
     return $this->view('users.edit')->with(['user' => $user, 'currentRoles' => $currentRoles, 'roles' => $roles]);
 }
 /**
  * Display the screen for picking the report
  *
  * @return Response
  */
 public function getRegistrationsFull()
 {
     // get orgs and beneficiaries
     $user = Auth::user();
     if (Auth::user()->access(UserType::SITE_ADMIN_LEVEL)) {
         $orgs = $this->organization->all();
     } else {
         $orgs = new Collection($user->organization->thisAndAllDescendentOrganizations());
     }
     // get weekly start and end dates
     $now = new Carbon();
     $start = Carbon::MONDAY == $now->dayOfWeek ? $now : $now->next(Carbon::MONDAY);
     $end = clone $start;
     $end->addWeeks(2);
     // display page
     return View::make($this->package . '::backend.' . $this->modelName . '.registrations-full', ['page_title' => 'Full Export', 'orgs' => $orgs->sortBy('name'), 'start' => $start, 'end' => $end]);
 }
 /**
  * @inheritdoc
  */
 public function ordering()
 {
     if ($this->orderCallback) {
         call_user_func($this->orderCallback, $this);
         return;
     }
     foreach ($this->request->orderableColumns() as $orderable) {
         $column = $this->getColumnName($orderable['column']);
         $this->collection = $this->collection->sortBy(function ($row) use($column) {
             $data = $this->serialize($row);
             return Arr::get($data, $column);
         });
         if ($orderable['direction'] == 'desc') {
             $this->collection = $this->collection->reverse();
         }
     }
 }
 /**
  * @return Collection
  */
 public function getPlugins()
 {
     if (is_null($this->plugins)) {
         $plugins = new Collection();
         $installed = [];
         $resource = opendir(base_path('plugins'));
         // traverse plugins dir
         while ($filename = @readdir($resource)) {
             if ($filename == "." || $filename == "..") {
                 continue;
             }
             $path = base_path('plugins') . "/" . $filename;
             if (is_dir($path)) {
                 if (file_exists($path . "/package.json")) {
                     // load packages installed
                     $installed[$filename] = json_decode($this->filesystem->get($path . "/package.json"), true);
                 }
             }
         }
         closedir($resource);
         foreach ($installed as $path => $package) {
             // Instantiates an Plugin object using the package path and package.json file.
             $plugin = new Plugin($this->getPluginsDir() . '/' . $path, $package);
             // Per default all plugins are installed if they are registered in composer.
             $plugin->setInstalled(true);
             $plugin->setNameSpace(Arr::get($package, 'namespace'));
             $plugin->setVersion(Arr::get($package, 'version'));
             $plugin->setEnabled($this->isEnabled($plugin->name));
             $plugins->put($plugin->name, $plugin);
         }
         $this->plugins = $plugins->sortBy(function ($plugin, $name) {
             return $plugin->name;
         });
     }
     return $this->plugins;
 }
Example #18
0
 /**
  * @return Collection|\Illuminate\Database\Eloquent\Collection
  */
 public function getGalleryItems()
 {
     if (!isset($this->galleryItems)) {
         // Get all images
         $this->model->getImages($this->fieldName);
         $this->galleryItems = $this->model->images()->where('image_type', $this->fieldName)->get();
         if ($this->model->allowsVideo()) {
             $collection = new Collection();
             $videos = $this->model->videos()->where('field', $this->fieldName)->get();
             if (count($videos)) {
                 foreach ($this->galleryItems as $item) {
                     $collection->push($item);
                 }
                 foreach ($videos as $video) {
                     $collection->push($video);
                 }
                 $this->galleryItems = $collection->sortBy(function ($item) {
                     return $item->order;
                 });
             }
         }
     }
     return $this->galleryItems;
 }
Example #19
0
 /**
  * @return Collection
  */
 public function getExtensions()
 {
     if (is_null($this->extensions) && $this->filesystem->exists(public_path('vendor/composer/installed.json'))) {
         $extensions = new Collection();
         // Load all packages installed by composer.
         $installed = json_decode($this->filesystem->get(public_path('vendor/composer/installed.json')), true);
         foreach ($installed as $package) {
             if (Arr::get($package, 'type') != 'flarum-extension' || empty(Arr::get($package, 'name'))) {
                 continue;
             }
             // Instantiates an Extension object using the package path and composer.json file.
             $extension = new Extension($this->getExtensionsDir() . '/' . Arr::get($package, 'name'), $package);
             // Per default all extensions are installed if they are registered in composer.
             $extension->setInstalled(true);
             $extension->setVersion(Arr::get($package, 'version'));
             $extension->setEnabled($this->isEnabled($extension->getId()));
             $extensions->put($extension->getId(), $extension);
         }
         $this->extensions = $extensions->sortBy(function ($extension, $name) {
             return $extension->composerJsonAttribute('extra.flarum-extension.title');
         });
     }
     return $this->extensions;
 }
Example #20
0
 /**
  * Get collection of Group instances sorted by their weight
  * @return Collection|Group[]
  */
 public function getGroups()
 {
     return $this->groups->sortBy(function (Group $group) {
         return $group->getWeight();
     });
 }
Example #21
0
    /*
     * la checkbox se รจ falsa non viene inviata
     *
     * ci sono soluzioni migliori?
     */
    if (!$request->request->has('finale')) {
        $finale = 0;
    } else {
        $finale = 1;
    }
    /*
     * inserisco una nuova riga csv
     */
    $writer->insertOne([$request->request->get('giorno'), $request->request->get('gara'), $finale]);
}
/*
 * il ritorno sono tutte le righe del csv ordinate per giorno
 *
 * Vedere esercizio 03
 */
$reader = Reader::createFromFileObject(new SplFileObject('file.csv'));
$results = iterator_to_array($reader->fetchAssoc(0), false);
use Symfony\Component\HttpFoundation\JsonResponse as Response;
use Illuminate\Support\Collection;
$collezione = new Collection($results);
$collezione->sortBy(function ($item, $key) {
    $data = \Carbon\Carbon::createFromFormat('d/m/y', $item['giorno']);
    return $data->timestamp;
});
$response = new Response($collezione);
$response->send();
Example #22
0
 /**
  * @return Collection|Item[]
  */
 public function getItems()
 {
     return $this->items->sortBy(function (Item $item) {
         return $item->getWeight();
     });
 }
Example #23
0
 /**
  * Show the form for editing the specified resource.
  *
  * @param int $id
  *
  * @return Response
  */
 public function edit($id)
 {
     if (!($model = $this->user->find($id))) {
         Flash::error(trans('dashboard::dashboard.errors.user.found'));
         return redirect()->route('users.index');
     }
     $currentRoles = $model->getRoles()->lists('name');
     if (empty($currentRoles)) {
         $currentRoles = new Collection(['name' => 'Not Available']);
     }
     $currentRoles->sortBy('name');
     $currentRoles = implode(', ', $currentRoles->toArray());
     $roles = $this->roles;
     return $this->view('users.edit', compact('model', 'currentRoles', 'roles'));
 }
Example #24
0
 /**
  * @param \Illuminate\Support\Collection $files
  * @return \Illuminate\Support\Collection
  */
 private function sortFilesByCreationDate($files)
 {
     return $files->sortBy(function ($file) {
         return $this->filesystem->getTimestamp($file['path']);
     });
 }
Example #25
0
 /**
  * Modifies the collection to be sorted by the orderByField and
  * orderByDirection properties.
  *
  * @param Collection $collection
  *
  * @return $this|Collection
  */
 private function processCollectionOrderBy(Collection $collection)
 {
     $field = $this->getOrderByField();
     $direction = $this->getOrderByDirection();
     $desc = false;
     if ($direction === 'desc') {
         $desc = true;
     }
     return $collection->sortBy(function ($entry) use($field) {
         if (property_exists($entry, $field)) {
             return $entry->{$field};
         }
     }, SORT_REGULAR, $desc);
 }
 public function setSorting($field, $direction)
 {
     $this->collection = $this->collection->sortBy($field, SORT_NATURAL, strtolower($direction) == 'desc');
     return $this;
 }
 /**
  * Creates a custom data collection of products
  *
  * @param $data
  *
  * @return Collection
  */
 public function createCustomProductCollection($data)
 {
     $collection = new Collection();
     foreach ($data as $subcategory) {
         foreach ($subcategory->products as $product) {
             $collection->push($product);
         }
     }
     // we just sort them alphabetically
     return $collection->sortBy(function ($p) {
         return $p->name;
     });
 }
Example #28
0
 /**
  * Sort the menu items by their weight.
  *
  * @return void
  */
 private function sortItems()
 {
     $this->items = $this->items->sortBy(function ($item) {
         return $item->weight;
     });
 }