前言

最近项目需要开发一个排行榜功能,根据订单金额进行排名,同金额排名相同,不同则跳过,序列递增。

技术实现

MySQL
通过SQL语句也能实现,不过SQL过于复杂,也不好维护。

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
SELECT
CASE
            
      WHEN
            @pre = final_score THEN
                  @pic + 0 
                  WHEN @pre := final_score THEN
                  @pic := @pic + 1 ELSE @pic := @pic + 1 
            END AS rank,
            rr.id registrationRecordId,
            rr.final_score AS number,
            us.userName,
            us.id userId,
            us.avatarImageURL headPortrait,
            CAST( p.`projectName` AS CHAR CHARSET UTF8 ) AS projectName,
            p.thumbnailURL,
            p.id projectId 
      FROM
            registration_record rr
            INNER JOIN `user` us ON rr.user_id = us.id
            INNER JOIN project p ON p.id = rr.project_id,(
            SELECT
                  @pre := NULL,
                  @pic := 0 
            ) AS init 
      WHERE
            rr.final_score IS NOT NULL 
            AND rr.competition_id = 41 
ORDER BY
      rr.final_score DESC

SQL类似这样,光从可读性就能劝退很多同学了。

Redis
Redis也能实现排行榜功能,主要通过zset中的分数特性来实现,不过对于我的业务不太适合 SQL+Java代码
通过SQL中的order by将查询的List根据某字段进行排好序,再将此List通过Java代码实现最终排行榜功能(推荐使用)

text
1 2 3 4 5 6 7 8 9
//伪SQL
select 
    a.schoolId
    a.schoolName,
    sum(a.amount)as count
    from a
    group by a.schoolId
    order by count desc

将需要排序的集合通过某字段排好序
Java代码

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/**
    * 成交金额排名
    *
    * @param amountRankList
    * @return
    */
   private static List<AmountRankVO> amountRank(List<AmountRankVO> amountRankList) {
      amountRankList.sort((s1, s2) -> -Float.compare(s1.getCount(), s2.getCount()));
      int index = 0;
      int count = 0;

      int tmpSize = 0;
      Float lastCount = -1.00f;
      List<AmountRankVO> tmpAmountRankList = new ArrayList<>();
      for (int i = 0; i < amountRankList.size(); i++) {
         AmountRankVO amountRankVO = amountRankList.get(i);
         if (Double.compare(lastCount, amountRankVO.getCount()) != 0) {
            lastCount = amountRankVO.getCount();
            index = index + 1 + count;
            count = 0;
         }
         amountRankVO.setSequence(index);
         //相同并列,不同则跳过,序号一次递增
         if (tmpSize > 0) {
            if (amountRankVO.getCount() < amountRankList.get(tmpSize - 1).getCount()) {
               amountRankVO.setSequence(tmpSize + 1);
               index = tmpSize + 1;
            }
         }
         tmpAmountRankList.add(amountRankVO);
         tmpSize = tmpAmountRankList.size();
      }
      return tmpAmountRankList;
   }

如果想实现同金额相同排名,不出现序列自增,只需注释下面代码:

text
1 2 3 4 5 6 7 8
 //相同并列,不同则跳过,序号一次递增
         if (tmpSize > 0) {
            if (amountRankVO.getCount() < amountRankList.get(tmpSize - 1).getCount()) {
               amountRankVO.setSequence(tmpSize + 1);
               index = tmpSize + 1;
            }
         }

最后

最后我们看一下具体实现的效果,类似功能比如成绩排名,销量排名等都能适用
在这里插入图片描述

原文地址:https://blog.csdn.net/qq_40553042/article/details/117591490?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168904443816800184167648%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168904443816800184167648&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-18-117591490-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96