php 中的复杂排名
Complex ranking in php
我一直在与一个复杂的排名作斗争 system.The 排名取决于学生的分数,这与分数无关。积分计算对我来说没问题。打破联系时会出现问题,因为这取决于完全不同的学生成绩。学生可以有相似的分数,但分数不同,可以用来打破平局,以防一些学生打平。我需要帮助,因为我被困住了。请参考我的图片得到一个粗略的想法,并告诉我是否可以完成。我还包含了一些我的代码。
<?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 :-(
?>
写一个比较函数,比较标记,通过比较点来打破平局。然后用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');
将学生分数作为较小的分数添加到 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;
}
像现在一样排序
我一直在与一个复杂的排名作斗争 system.The 排名取决于学生的分数,这与分数无关。积分计算对我来说没问题。打破联系时会出现问题,因为这取决于完全不同的学生成绩。学生可以有相似的分数,但分数不同,可以用来打破平局,以防一些学生打平。我需要帮助,因为我被困住了。请参考我的图片得到一个粗略的想法,并告诉我是否可以完成。我还包含了一些我的代码。
<?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 :-(
?>
写一个比较函数,比较标记,通过比较点来打破平局。然后用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');
将学生分数作为较小的分数添加到
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; }
像现在一样排序