Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
This is does not answer using the same type of example, but it is a generic sorter that may do what you need. sandbox.onlinephpfunctions.com/code/… –  Brian Jun 11 at 3:29
    
By the way... Why are you defining a SimpleHeapSort at the top of your example and then never using it? –  Brian Jun 11 at 3: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 at 3:42
    
I think... If you were using it there, it would look like $heap = SimpleHeapSort(); –  Brian Jun 11 at 8:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.