1

I'm working on CakePHP. I have this array

     Array
(
    [0] => Array
        (
            [Event] => Array
                (

                    [start_date] => 2014-01-23

                    [year] => 2014
                )
        )

    [1] => Array
        (
            [Event] => Array
                (

                    [start_date] => 2015-01-23
                    [year] => 2015
                )
        )

    [2] => Array
        (
            [Event] => Array
                (

                    [start_date] => 2015-02-23

                    [year] => 2015
                )
        )

    [3] => Array
        (
            [Event] => Array
                (  
                    [start_date] => 2016-02-01
                    [year] => 2016
                )
        )

    [4] => Array
        (
            [Event] => Array
                (
                    [start_date] => 2016-02-02
                    [year] => 2016
                )
        )
)

I want to this array asc on start_date, but I want to sort year as desc. I have done

 $History = Set::sort($eventSortedHistory, '{n}.Event.start_date', 'asc');
     $History = Set::sort($eventSortedHistory, '{n}.Event.year', 'desc');

obviously, it will sort but by year.

So,How can I sort year as desc and start_date as asc in PHP?

Aditya Harshey
  • 170
  • 1
  • 13

1 Answers1

1

I think you might be looking for array_multisort() php function.

If you want to use Set::sort you will need to create a custom function to achieve that because it isn't meant to sort arrays by multiple fields.

Solved issue about array_multisort()

Hope this helps you :)

Community
  • 1
  • 1
Asur
  • 379
  • 3
  • 20