Skip to content

[5.6] Add sortKeys and sortKeysDesc methods to Collection.#23286

Merged
taylorotwell merged 1 commit into
laravel:5.6from
pmatseykanets:collection-sortkeys
Feb 26, 2018
Merged

[5.6] Add sortKeys and sortKeysDesc methods to Collection.#23286
taylorotwell merged 1 commit into
laravel:5.6from
pmatseykanets:collection-sortkeys

Conversation

@pmatseykanets
Copy link
Copy Markdown
Contributor

This PR adds two convenience methods to Collection to allow sorting keys of the collection, or sorting the collection by the key if you wish.

  • sortKeys
  • sortKeysDesc

One of the use cases that I find myself a lot lately is grabbing an object via an API and rendering it's properties in a sorted fashion.

This PR allows to do something along the lines of

$this->renderLead(collect($this->getLeadViaAPI($id))->sortKeys());

instead of

$leadData = get_object_vars($this->getLeadViaAPI($id));
ksort($leadData);

$this->renderLead(collect($leadData));

or doing some unnatural combination of map and sort.

@tillkruss tillkruss changed the title Add sortKeys and sortKeysDesc methods to Collection. [5.6] Add sortKeys and sortKeysDesc methods to Collection. Feb 25, 2018
@taylorotwell taylorotwell merged commit 41da426 into laravel:5.6 Feb 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants