Sort items from a Collection from a preset value

Asked

Viewed 851 times

4

I have a Collection of the Eloquent and I would like to order it through two fields at the same time being one of them a predefined value.

Ex.: this one Collection has several objects of the type Categoria and I want to order it so that the categories that have the property slug equal to "solicitacoes" stay at the beginning and I want the others to be in alphabetical order.

I know the method sort may receive a callback to sort the collection and tried to do (among other things) this

$sorted = $categorias->sort(function ($item, $next) {
    return $item->slug === 'solicitacoes' ? -1 :            
           strcmp($item->descricao, $next->descricao);
});

But the ordination did not work very well, disregarding the categories with slug = "solicitacoes" was in alphabetical order, the problem is that the aforementioned ones were not in the beginning.

  • strcmp returns the difference of the characters in what differs. Example strcmp of "A" for "Z" return -25. I’d advise you to experiment with -100 to stay before all.

  • @Isac I tried with -10000 and gave in the same.

  • @Djalmamanfrin I didn’t understand a word you meant.

  • If you pass the sorted data alphabetically to the collection and then use the $sorted = $categorias->sortBy('solicitacoes'), which result would come out ofcollection $categorias ? I started again because I couldn’t change the previous comment.

  • @Djalmamanfrin There is no sortBy('solicitacoes') since solicitacoes is a value of slug. If I understand correctly you want me to bring the results of the bank already ordered alphabetically and then try to do my custom ordering, if that’s it, it doesn’t work because the second ordering undoes the first.

  • And dd($item->slug) presents what ? for a request

  • @Isac Displays the field value slug. May solicitacoes, downloads, anything. To be a request the field returns solicitacoes.

  • My question was more to make sure you’re getting into if and return -1/-10000 for requests, do not have any space or odd character that makes the condition not enter, or until it is of a different type once it is being used ===

  • Oh yes, he’s right. No strange character or anything.

Show 4 more comments

2 answers

3


Using the Collection, i replaced the method sort for SortBy.

The method sortBy causes Laravel to internally compare values according to types, through the sort (something like what you asked in the question using the sort).

So we can check on sortBy if the value of slug is equivalent to "solicitações". If so, we return NULL so that this item is placed above. Otherwise, we return "descricao" to sort by that field.

Behold:

$sorted = $categorias->sortBy(function ($item)
{
     return $item->slug === 'solicitacoes' ? null : $item->descricao;
});

1

  • 1

    Good exit, but I thought you had something more readable.

  • Good @LINQ I see no alternative when it is to order including two fields that is your case, if order only one gives, but, what I did will order the two fields and that I think was not clear in your question.!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.