Handling ties when ranking from the highest to the lowest

前端 未结 2 1157
北荒
北荒 2021-01-26 00:39

I am trying to make a ranking manager for a small project.The totals are stored in the database.I can easily get the max and min using mysql and also arrange the records descend

2条回答
  •  情书的邮戳
    2021-01-26 00:54

    $data = array(
      'A'=>19,'B'=>18,'C'=>17,'D'=>17,'E'=>16,'F'=>15
    );
    
    $rank = 0;
    $lastScore = PHP_INT_MAX;
    foreach( $data as $name=>$score ) {
      if ( $lastScore !== $score ) {
        $lastScore = $score;
        $rank += 1;
      }
      printf("%s %d (%d)\n", $name, $score, $rank);
    }
    

    prints

    A 19 (1)
    B 18 (2)
    C 17 (3)
    D 17 (3)
    E 16 (4)
    F 15 (5)
    

提交回复
热议问题