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.

Hi I have to sort this array in desc. order on the basis of ratingVal using php. How can I do that.

Array
(
    [0] => Array
        (
            [rating_postid] => 26
            [raCnt] => 6
            [sRate] => 18
            [ratingVal] => 3
        )

    [1] => Array
        (
            [rating_postid] => 714
            [raCnt] => 3
            [sRate] => 14
            [ratingVal] => 4.6666666666667
        )

    [2] => Array
        (
            [rating_postid] => 14
            [raCnt] => 4
            [sRate] => 12
            [ratingVal] => 3
        )

    [3] => Array
        (
            [rating_postid] => 290
            [raCnt] => 2
            [sRate] => 10
            [ratingVal] => 5
        )

    [4] => Array
        (
            [rating_postid] => 194
            [raCnt] => 2
            [sRate] => 8
            [ratingVal] => 4
        )

    [5] => Array
        (
            [rating_postid] => 134
            [raCnt] => 2
            [sRate] => 6
            [ratingVal] => 3
        )

    [6] => Array
        (
            [rating_postid] => 707
            [raCnt] => 1
            [sRate] => 5
            [ratingVal] => 5
        )

)
share|improve this question
    
I retagged your question to use more appropriate tags, e. g. dont use the PHP 5.3 tag if you actually do use 5.2.X. –  Max Oct 27 '10 at 10:56
add comment

3 Answers

up vote 3 down vote accepted

Use usort:

function sortIt( $a, $b ){
    return $b['ratingVal'] - $a['ratingVal'];
}

usort( $yourArray, "sortIt" );

For ascending order, swap the $a and $b in the subtraction.

The usort function allows you to create your own custom sort function

share|improve this answer
    
Thank you sir :) –  learner Oct 27 '10 at 11:31
    
+1 (a) because your code is the way to do this and (b) because I stole it for my answer! –  lonesomeday Oct 27 '10 at 11:45
1  
Any reason you didn't use function sortIt($a, $b) {return $b['ratingVal']-$a['ratingVal'];} ? –  outis Oct 27 '10 at 13:15
add comment

If you retrieve this data from mysql database (as I see, mysql tag is specified), you may use ORDER BY statement in your query.

If this is not suitable, you'll have to implement comparison function and use usort() function.

share|improve this answer
add comment

You've tagged your code "php5.3", so I'm going to give a 5.3-specific answer.

Adapting Harmen's code:

usort( $yourArray, function($a, $b) {
    if( $a['ratingVal'] == $b['ratingVal'] )
        return 0;
    return ( $a['ratingVal'] < $b['ratingVal'] ) ? 1 : -1;
} );

This uses usort and anonymous functions, a new addition in PHP 5.3.

share|improve this answer
    
sorry sir I am using php 5.2.6 –  learner Oct 27 '10 at 10:50
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.