2016-01-21 111 views
1

我一直在努力爭取一些複雜的排名系統。排名取決於學生獨立於分數的分數。點計算對我來說沒問題。問題出現在打破關係時,因爲這取決於完全不同的學生標記。學生可以有相似的分數,但分數有所不同,可以用來打破平局,以防一些學生在積分上打分。我需要幫助,因爲我卡住了。請參考我的照片以獲得一個粗略的想法,並告訴我是否可以完成。我還包括了一些我的代碼。php中的複雜排名

A visual illustration

<?php 
$rank_by_point=array(); 
//loop out students and get their admission number from which you calculate individual points based on an algorithm 
foreach($students as $student => $student_no){ 
array_push($rank_by_point[$student_no],calculatePoints($student_no)); 
} 
//sorting the points from highest to lowest 
arsort($rank_by_point); 

//the ranking code down here 
$ties=array(); 
$break=array(); 
$initial_positions=array(); 
$rank = 0; 
$last = false; 
foreach($rank_by_point as $key => $value){ 
    if($last != $value){ 
    $last = $value; 
    $rank++;     
    }else{ 
    //when a tie is detected add the values to a tie array 
    array_push($ties[$key],$rank); 
    } 
    array_push($initial_positions[$key],$rank); 
} 
//spliting of ties is done by getting marks and rearrangin the tie 
foreach($ties as $admno => $st_rank){ 
    $read_position=$rank;//same value for all keys in the ties array 
    $getmarks=getMarks($admno); 
    array_push($break[$admno],$getmarks); 
    //reorder the array in desc order 
    arsort($break); 
} 
//reranking the ties. i got stuck here :-(
?> 

回答

2
  1. 添加學生標記爲一個較小的比分進入calculatePoints($student_no)。例如

    function calculatePoints($student_no){ 
    
        /*add this before return it*/ 
    
        $point = $point*1000;//make sure the new $point be bigger than the max $student_mark 
    
        $point += $student_mark; 
    
        return $point; 
    } 
    
  2. 排序,你現在做

2

寫一個比較標誌,並打破了由比較點關係的比較功能。然後用usort用這個函數對$students數組進行排序。

function compare_students($s1, $s2) { 
    $p1 = calculatePoints($s1); 
    $p2 = calculatePoints($s2); 
    if ($p1 == $p2) { 
     return getMarks($s1) - getMarks($s2); 
    } else { 
     return $p1 - $p2; 
    } 
} 

usort($students, 'compare_students'); 
+0

你好。在使用分數進行排名時,關聯使用標記來打破。我確實嘗試了代碼的修改版本,但訂購那些綁定了 –

+0

的訂單時仍存在問題。 – Barmar