Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom sorting of toHierarchy result #46

Closed
wants to merge 3 commits into from
Closed

allow custom sorting of toHierarchy result #46

wants to merge 3 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Mar 4, 2014

allow custom sorting of toHierarchy result, using a arbitrary comparator
function with uasort. Fixes #41.
Possibly not as efficient as a pure SQL solution depending on the
complexity of the sort and the number or records but for simple
comparison its fairly elegant.

example to sort by "name" attribute of node

$sorted = Category::all()->toHierarchy(function($a,$b){ return
strcmp($a->name, $b->name);})

shaddowgh added 3 commits March 4, 2014 14:04
allow custom sorting of toHierarchy result, using a arbitrary comparator
function with uasort. Fixes #41.
Possibly not as efficient as a pure SQL solution depending on the
complexity of the sort and the number or records but for simple
comparison its fairly elegant.

example to sort by "name" attribute of node
```
$sorted = Category::all()->toHierarchy(function($a,$b){ return
strcmp($a->name, $b->name);})
```
set children relation to empty array for leaf nodes to prevent them from
being re-queried when iterating over tree in template or such.
tests expect a collection object as they use its count function so
return an empty collection rather than an empty array.
@ghost ghost mentioned this pull request Mar 4, 2014
@tremby
Copy link

tremby commented Mar 4, 2014

This fixes an issue I have in #45; I'll vote for its inclusion.

@etrepat
Copy link
Owner

etrepat commented May 2, 2014

There's now a way to implement custom sorting in the library. Will that suffice?

I think it's rather preferable to get the results collection sorted from the database (which is faster) than sorting it client-side.

@etrepat
Copy link
Owner

etrepat commented May 13, 2014

I'm sorry but the toHierarchy method has been rewritten entirely and merging this is just not possible.

Also, now it is possible for you to use the provided sort operations on the Collection object and toHierarchy will preserve that ordering: $category->descendants()->get()->sort( ... callback here ...)->toHierarchy(), should do something similar to what you wanted to achieve.

@etrepat etrepat closed this May 13, 2014
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.

Sorting Nodes?
2 participants