Re: Sorting multidim array and keeping associations

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

 



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.

/Peter
Array
(
    [40] => Array
        (
            [1] => 16
            [2] => 20
            [3] => 20
            [4] => 10
            [total] => 66
        )

    [35] => Array
        (
            [1] => 20
            [2] => 11
            [3] => 12
            [4] => 20
            [5] => 17
            [total] => 80
        )

    [49] => Array
        (
            [2] => 14
            [total] => 14
        )

    [139] => Array
        (
            [4] => 14
            [total] => 14
        )

    [79] => Array
        (
            [4] => 10
            [5] => 4
            [total] => 14
        )
)

//Sorting Order Of Merit arrayfunction sort_oom($arr) { //create the sum
element by adding all tournament result foreach($arr as $id => $player) {
$total=0;  foreach($player as $tourpoint) {   $total+=$tourpoint;  }
$arr[$id]['total']=$total; }  reset($arr); $num = count($arr);
$sortedarray=array(); for($i=1; $i<=$num; $i++) {  //Get the element that
ranks currently #1  $elem = get_largest_element($arr, 'total');  //Put that
element into a new array  $sortedarray[$elem]=$arr[$elem];  //Unset the
element from the original array  unset($arr[$elem]);  //reset the array
reset($arr); } //Viola, the array is sorted... return $sortedarray; }
function get_largest_element($arr, $sortkey) { //create candidatearray $cand
= array(); //before looping the largest total is 0 $largest = 0;
reset($arr); foreach($arr as $id => $value) {  if($value[$sortkey] >
$largest) {   //Delete all candidates entered because they are not
candidates to be largest no more   unset($cand);   //Insert into
candidatearray   $cand[$id]=$value;   //update the largest value   $largest
= $value[$sortkey];  } elseif($value[$sortkey] == $largest) {   //Add to
candidate key   $cand[$id]=$value;  } } $arg=true; $largest=0; reset($cand);
//Candidate contains at minimum 1 candidate //If more than two candidates,
subsorting have to begin while(count($cand)>1 AND $arg) {  //Find the
largest subvalue (all values not $sortkey)  foreach($cand as $id => $value)
{   foreach($value as $secid => $point) {    if($point > $largest AND
$secid!=$sortkey) {     $largest=$point;    }   }  }  //Loop thru array and
delete elements that do not have  //values larger or equal to the largest
value  foreach($cand as $id => $value) {   $ok=false;   foreach($value as
$secid => $point) {    if($point == $largest AND $secid!=$sortkey) {
unset($cand[$id][$secid]);     $ok=true;    }   }   if(!$ok AND
count($cand[$id])>1) { unset($cand[$id]);}  }  //If there are candidates
presice equal, pick the first.  if($largest==0 AND count($cand)>1) {
$arg=false;   $check = 1;   foreach($cand as $id => $value) {
if($check!=1) {     unset($cand[$id]);    }    $check++;   }  }
 $largest=0; } reset($cand); $el = each($cand); return $el['key'];}

-- 
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