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?

1 Answer 1

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 :)

Sign up to request clarification or add additional context in comments.

Comments

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.