Laravel Collection Sort By Multiple Fields Example - TechvBlogs

Laravel Collection Sort By Multiple Fields Example

In this article, You will learn how to sort array by multiple fields using Laravel collection.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

4 months ago

TechvBlogs - Google News

In this example, I'll guide you through sorting Laravel collections by multiple fields. Gain insights into the concept of ordering Laravel collections by multiple columns. I'll provide a clear example demonstrating how to use the sortby method to achieve multi-column sorting in Laravel collections. Follow the steps below to effectively implement sorting by multiple columns in Laravel collections.

Laravel Collection Sort By Multiple Fields Example

Laravel simplifies the process of sorting multiple fields in a collection with the sortBy() method. This method accepts an array argument, allowing you to specify multiple columns for ordering within the collection. Below, I'll provide two examples to illustrate the usage:

Laravel Collection Sort By Multiple Fields Example 1:

You can see the below controller code:

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $collection = collect([
            ['id' => 1, 'name' => 'Smit', 'age' => '25'],
            ['id' => 2, 'name' => 'Suresh', 'age' => '26'],
            ['id' => 3, 'name' => 'Vishal', 'age' => '23'],
            ['id' => 4, 'name' => 'Bhavik', 'age' => '21']
        ]);
   
        $sorted = $collection->sortBy(['age', 'name']);
    
        $sorted = $sorted->all();
       
        dd($sorted);
    }
}

Output:

Array
(
    [3] => Array
        (
            [id] => 4
            [name] => Bhavik
            [age] => 21
        )
    [0] => Array
        (
            [id] => 3
            [name] => Vishal
            [age] => 23
        )
    [2] => Array
        (
            [id] => 1
            [name] => Smit
            [age] => 25
        )
    [1] => Array
        (
            [id] => 2
            [name] => Suresh
            [age] => 26
        )
)

Laravel Collection Sort By Multiple Fields Example 2:

You can see the below controller code:

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $collection = collect([
            ['id' => 1, 'name' => 'Smit', 'age' => '25'],
            ['id' => 2, 'name' => 'Suresh', 'age' => '26'],
            ['id' => 3, 'name' => 'Vishal', 'age' => '23'],
            ['id' => 4, 'name' => 'Bhavik', 'age' => '21']
        ]);
     
        $sorted = $collection->sortBy(function ($value) {
            return $value['age'].'-'.$value['name'];
        });
      
        $sorted = $sorted->all();
      
        dd($sorted);
    }
}

 Output:

Array
(
    [3] => Array
        (
            [id] => 4
            [name] => Bhavik
            [age] => 21
        )
    [0] => Array
        (
            [id] => 3
            [name] => Vishal
            [age] => 23
        )
    [2] => Array
        (
            [id] => 1
            [name] => Smit
            [age] => 25
        )
    [1] => Array
        (
            [id] => 2
            [name] => Suresh
            [age] => 26
        )
)

Thank you for reading this guide!

Comments (0)

Comment


Note: All Input Fields are required.