4

I want to be able to sort the results of my collection by the status column in specific order.

The idea is to paginate the collection but giving priority in a specific order: [2, 1, ...], so the pending listings are shown first and the other after!

Does anyone has an idea on such method?

Thanks in advance.

3
  • 1
    post your code of collection, how it looks like Commented May 17, 2020 at 9:43
  • Does this answer your question? Sorting Laravel Collection via Array of ID's Commented Jan 7, 2022 at 21:51
  • @miken32 thanks for the reply, kindly check the marked answer bellow, it is exactly what I wanted :D Commented Jan 9, 2022 at 11:43

2 Answers 2

9

If your status priority is not in numeric order (e.g. 2 > 1 > 3 > 0), you can alternatively pass a callback function to sortBy:

$collection = collect([
    ['name' => 'A', 'status' => 0],
    ['name' => 'B', 'status' => 3],
    ['name' => 'C', 'status' => 0],
    ['name' => 'D', 'status' => 1],
    ['name' => 'E', 'status' => 2],
    ['name' => 'F', 'status' => 1],
    ['name' => 'G', 'status' => 3],
    ['name' => 'H', 'status' => 1],
    ['name' => 'I', 'status' => 2],
    ['name' => 'J', 'status' => 3],
]);

// define status priority here
$statusPriorities = [2, 1, 3, 0];

$collection->sortBy(function($order) use($statusPriorities){
   return array_search($order['status'], $statusPriorities);
})->values()->all();

Output:

[
     [
       "name" => "I",
       "status" => 2,
     ],
     [
       "name" => "E",
       "status" => 2,
     ],
     [
       "name" => "D",
       "status" => 1,
     ],
     [
       "name" => "F",
       "status" => 1,
     ],
     [
       "name" => "H",
       "status" => 1,
     ],
     [
       "name" => "B",
       "status" => 3,
     ],
     [
       "name" => "G",
       "status" => 3,
     ],
     [
       "name" => "J",
       "status" => 3,
     ],
     [
       "name" => "A",
       "status" => 0,
     ],
     [
       "name" => "C",
       "status" => 0,
     ],
   ]
Sign up to request clarification or add additional context in comments.

2 Comments

how would I set about doing that but with eloquent (model) and with paginate?
Important: when not all possible values are listed in the prioritized order list, additionally use in_array, otherwise those not found values will be at the beginning.
1

Referring to laravel documentation https://laravel.com/docs/7.x/collections#method-sortby you can sort collection by column like...

$tasks = App\Task::all();
$tasks1 = $tasks->sortBy('title');
return $tasks1->values()->all();
//values() method is used to preserve array original keys
//You can also user sortByDesc, to get reverse order of your collection.

As you can see it has been sorted by title in asc order

[
    {
        "id": 5,
        "title": "Akhtar",
        "created_at": "2019-09-01 14:18:05",
        "updated_at": "2019-09-01 14:18:05"
    },
    {
        "id": 6,
        "title": "Hamid khan",
        "created_at": "2019-09-01 14:18:05",
        "updated_at": "2019-09-01 19:19:18"
    },
    {
        "id": 7,
        "title": "Zarif",
        "created_at": "2019-09-04 19:14:18",
        "updated_at": "2019-09-04 19:14:18"
    },
    {
        "id": 1,
        "title": "testing",
        "created_at": "2019-09-01 13:01:39",
        "updated_at": "2019-09-01 13:01:39"
    }
]

4 Comments

I appreciate your input, but this answer is not what I was looking for!
It's exactly the same thing as above answer. but its okay not a problem
It isn't, the idea was to use an array to set the order of priorities on that column... @CBrach answered it perfectly!
I was also going that way, and i was doing the samething i was editing my answer. but np dear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.