Sorting Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hchsk
Forum Newbie
Posts: 2
Joined: Fri May 01, 2009 6:40 pm

Sorting Help

Post by hchsk »

Hello,


I am trying to sort an array of integers by value (these are scores), and return an array with keys defined by their place in the original list (their team number), and element values equal to their sorted order (the team rank).

This I have accomplished with:

Code: Select all

 
//OPEN AND FIX GLITCH
$filename = "scorestore.txt";
$score = file($filename);
for ($counter = 0; $counter < count($score); $counter +=1) {$score[$counter] = intval($score[$counter]);}
$scores=$score;
 
//APPEND TEAM NUMBER TO SCORE
for ($counter = 0; $counter < count($score); $counter +=1) {$hold = key($scores)+1; $scores[$counter] = $scores[$counter] . '.' . $hold; next($scores);}
 
//SORT
rsort($scores);
 
//REMOVE SCORE
for ($counter = 0; $counter < count($score); $counter +=1)  {if (strlen(floor($scores[$counter]))==1) {$scores[$counter] = substr_replace($scores[$counter], '', 0, 2);}
                                     else {$scores[$counter] = substr_replace($scores[$counter], '', 0, 3);}
                                    }
 
//SWITCH to FORMAT: $scores[team#] = place
$scores = array_flip($scores);
 
Here's my problem:

I want to allow for teams (keys) with equal original values (scores) to have equal new values (ranking) equal to the lesser of the two. Basically, now that I have accomplished the first part, I want to go back, check for equal values, and for every group of equal elements, ignore the first, and subtract 1 from the value of the rest. I would loop this count($score) many times, to go through all possible equal values.


Every attempt I have made has failed in some way, such as only working for ties between adjacent numbers or subtracting from all involved teams rather than skipping over the first. I would welcome any help or solution, including one that discards the successful code I have written in favor of a new, working, overall solution.

the scorestore.txt will equal something like this:

Code: Select all

3
3
0
4
10
1
5
9
10
10
 
Hopeful final output would be print_r($score); ==

Code: Select all

 
Array ( [9] => 1 [5] => 1 [10] => 1 [8] => 4 [7] => 5 [4] => 6 [2] => 7 [1] => 7 [6] => 9 [3] => 10 )
 

Thank you for your time and help.
Last edited by Benjamin on Fri May 01, 2009 9:07 pm, edited 1 time in total.
Reason: Changed code type from text to php.
neerajjaiswal
Forum Newbie
Posts: 1
Joined: Mon May 04, 2009 11:45 pm

Re: Sorting Help

Post by neerajjaiswal »

Hi,

find sorting code below :lol:

Code: Select all

 
<?php
 
/***'*** GET LINE SEPERATOR ***'***/
define("LINE_SEPERATOR","\n");
 
$filename = "scorestore.txt";
 
/***'*** READ FILE CONTENT IN STRING ***'***/
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
 
 
/***'*** GAT SCORE ARRAY ***'***/
$score = explode(LINE_SEPERATOR,$contents);
 
 
/***'*** SORT ON BASIS OF SCORE IN DESC ORDER ***'***/
 
$scores=$score;
arsort($scores);
 
if($result = teamRanking($scores)){
    var_dump($result);
} else {
    echo "Team Score is empity";
}
 
 
/***'*** RANKING ON BASIS OF SCORE ***'***/
 
function teamRanking($scores){
    if(gettype($scores)=='array'){
        $rank = 1;
        $tscore = -1;       //non score value flag
        $result = array();
 
        foreach($scores as $key=>$val){
 
            //First time entry or equal scoring team
 
            if($tscore == -1 || $tscore == $val){
                $tscore = $val;
                $result[$key] = $rank;
            } else {
                $tscore = $val;
                $rank = $rank + 1;
                $result[$key] = $rank;
            }
 
        }
 
    } else {
        return false;
    }
    return $result;
}
 
?>
 
Last edited by Benjamin on Tue May 05, 2009 2:12 am, edited 1 time in total.
Reason: Added [code=php] tags.
Post Reply