Re: Sorting multidim array and keeping associations

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux