1

Find statements in CakePHP produce an array structured as below. I've already ordered my search to produce the result set shown (ordered by combined_score). Now, I'd like to apply a sorting function on the data to sort by "average_votes". See "from this:"/"to this:" below.

I'd really appreciate any suggestions.

From this:
Array
(
[0] => Array
    (
        [Vehicle] => Array
            (
                [id] => 52
                [user_id] => 101
                [name] => Ford
                [total_votes] => 5
                [average_votes] => 3.8
                [combined_score] => 19
            )

    )

[1] => Array
    (
        [Vehicle] => Array
            (
                [id] => 48
                [user_id] => 101
                [name] => Nissan
                [total_votes] => 6
                [average_votes] => 5
                [combined_score] => 2
            )
    )
)

To this:
Array
(
[0] => Array
    (
        [Vehicle] => Array
            (
                [id] => 48
                [user_id] => 101
                [name] => Nissan
                [total_votes] => 6
                [average_votes] => 5
                [combined_score] => 2
            )
    )   

[1] => Array
    (
        [Vehicle] => Array
            (
                [id] => 52
                [user_id] => 101
                [name] => Ford
                [total_votes] => 5
                [average_votes] => 3.8
                [combined_score] => 19
            )

    )

)
5
  • Please look at the "Related" section on the right. Commented May 2, 2012 at 9:48
  • 2
    Why don't you have the database do the sorting? Commented May 2, 2012 at 9:49
  • Jon, which of these related links show how to sort by data 3 levels deep as shown above? Commented May 2, 2012 at 9:59
  • Ori, I have asked the database to sort by combined_score, and then with that result set, I need to sort by average_votes. Do you have any suggestions on how to do what you say? Commented May 2, 2012 at 10:00
  • Now you should have enough rep to self-answer your question. Commented Mar 1, 2016 at 23:14

2 Answers 2

2

Depending on what you're actually trying to achieve you can still do this at the database level in cake, like so:

$this->Vehicle->find('all',array('order' => array('Vehicle.combined_score' => 'asc', 'Vehicle.average_votes' => 'desc')));

Which will first sort by combined score, and then sort by average votes

The second option is to use the cakephp Set class, like so:

$results = Set::sort($results, '{n}.Vehicle.average_votes', 'desc');
Sign up to request clarification or add additional context in comments.

Comments

2

In your controller the find function you can add option for sorting.

$this->Vehicle->find('all',array('order' => array('Vehicle.average_votes DESC')));

1 Comment

Thanks gvLearner, but as I mentioned in my post, I need it first sorted by combined_score, and then I need to take that result set and resort it by average votes.

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.