0

I am trying to sort my results by a number value which is calculated OUTSIDE of the array - I see that other questions have been asked similar to mine but none mention sorting by a value given OUTSIDE of the array. I am currently trying to use SPL, but it does not sort results by highest to lowest - I am not sure why. Any thoughts would be appreciated.

class SimpleHeapSort extends SplMaxHeap {
    public function compare($a, $b) {
        return strcmp($a, $b);
    }
}



        $r_array = array();
        $obj = $this->request_data->d->results;
        $ic = count($obj);
        print("<table cellspacing=10>");
        for($i=0;$i<$ic;$i++){
            $r_array[$i] = array('Title'=>$obj[$i]->Title,'Description'=>$obj[$i]->Description,'Url'=>$obj[$i]->Url);
            $fb_likes[$i] = reset($this->get_fb_likes($r_array[$i]['Url']) );   
            $heap = new SplMaxHeap();
            $heap->insert($fb_likes[$i]->total_count);

                foreach($heap as $number) {
                    echo "<tr><td>".$r_array[$i]['Title']."</td>";
                    echo "<td>".$r_array[$i]['Description']."</td>";
                    echo "<td>".$r_array[$i]['Url']."</td>";
                    echo "<td>fb" . $number . "</td></tr>";
                }
            }
            print("</table>");

I would like to sort my results (highest to lowest) by $fb_likes->total_count - this is a number/integer. This value is calculated OUTSIDE of $r_array[$i]. How can I go about sorting each result from highest to lowest $fb_likes->total_count number value?

While I am currently trying to use SPL, I am open to any alternatives as well.

Thank you in advance for any help.

Alex
  • 196
  • 5
  • 22
  • This is does not answer using the same type of example, but it is a generic sorter that may do what you need. http://sandbox.onlinephpfunctions.com/code/af8579c8f037e039613216e6122c6525c6c82ea9 – Brian Jun 11 '14 at 03:29
  • By the way... Why are you defining a SimpleHeapSort at the top of your example and then never using it? – Brian Jun 11 '14 at 03:30
  • @Brian Thanks Brian. I'll take a look and let you know! Also, I am using it on this line of code: `$heap = new SplMaxHeap();` – Alex Jun 11 '14 at 03:42
  • I think... If you were using it there, it would look like `$heap = SimpleHeapSort();` – Brian Jun 11 '14 at 08:13

0 Answers0