Re: Sorting multidim array and keeping associations

From: Date: Thu, 18 Nov 2004 13:31:28 +0000
Subject: Re: Sorting multidim array and keeping associations
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-202416@lists.php.net to get a copy of this message
Peter Lauri wrote:
The problem was more complex than I described, I instead wrote a sorting algorithm for this specific purpose. The array multi-dim array looks like this (short version). The total element is just the sum of the other. This is a point system for golftournaments. I wanted to sort them first on "total" and if the total was the same I wanted to sort them with the highest point in a specific tournament, and so on. I have not found an simple solution to this using any built in functions. So I thought is was the best to sort it "manually". My algorithm is problably not efficient, but it works just fine for this solution. You can find the algorithm in the end of this message. Array (
    [40] => Array
        (
            [1] => 16
            [2] => 20
            [3] => 20
            [4] => 10
            [total] => 66
        )
    [49] => Array
        (
            [2] => 14
            [total] => 14
        )
)
// without totals $score_board = uksort( $score_board, 'sortScoreBoard' ); function sortScoreBoard( $element_1, $element_2 ) { if ( array_sum( $element_1 ) > array_sum( $element_2 ) ) {
       return -1;
} elseif ( array_sum( $element_1 ) < array_sum( $element_2 ) ) {
       return 1;
} elseif ( max( $element_1 ) > max( $element_2 ) ) {
       return -1;
} elseif ( max( $element_1 ) > max( $element_2 ) ) {
       return 1;
} return 0; } -- Sebastian Mendel www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com www.sf.net/projects/phpdatetime www.sf.net/projects/phptimesheet

« previous php.general (#202416) next »