其他分享
首页 > 其他分享> > List排序

List排序

作者:互联网

一、添加一个比较器

点击查看代码
import java.util.Comparator;

/**
 * @Classname ComparatorResultType
 * @Description 排序比较器(金牌师傅推荐-分页中使用)
 * @Date 2022/3/10 11:19
 * @Created by 小郭
 */
public class ComparatorResultType implements Comparator {
    @Override
    public int compare(Object arg0, Object arg1) {

        //MasterPageVO是查询数据的实体类,自己根据业务替换掉
        MasterPageVO masterPageVO0=(MasterPageVO)arg0;
        MasterPageVO masterPageVO1=(MasterPageVO)arg1;
        //首先比较主指标,如果主指标相同,则比较次指标
        int flag=masterPageVO0.getScore().compareTo(masterPageVO1.getScore());
        if(flag==0){
            return masterPageVO0.getOrderCount().compareTo(masterPageVO1.getOrderCount());

        }else{
            return flag;
        }
    }
}

二、业务代码

点击查看代码
@Override
    public PageData<MasterPageVO> getMasterPage(Map<String, Object> params) {

        List<MasterPageVO> voList=masterWorkerDao.getMasterPage(params);

        if (voList.size()>0 && !voList.isEmpty()) {
            for (MasterPageVO vo : voList) {
                //查询师傅拥有的服务
                List<MasterWorkerServiceTypeVO> serviceList = masterWorkerServiceTypeDao.getServiceByMasterWorkerId(Long.parseLong(vo.getId()));
                if (serviceList.size() > 0) {
                    vo.setServiceList(serviceList);
                }
                //查询印象评价标签列表
                List<ImpressionEvaluationVO> impressionEvaluationVOList = masterWorkerDao.getImpressionEvaluationList(Long.parseLong(vo.getId()));
                if (impressionEvaluationVOList.size() > 0) {
                    vo.setImpressionEvaluationVOList(impressionEvaluationVOList);
                }
                //查询评分(总评分数/接单量)
                ScoreDetailVO scoreDetailVO = masterWorkerDao.getScoreTotal(Long.parseLong(vo.getId()));
                if (scoreDetailVO.getScoreTotal() != null && scoreDetailVO.getOrderCount() != null) {
                    vo.setScore(Integer.parseInt(scoreDetailVO.getScoreTotal()) / Integer.parseInt(scoreDetailVO.getOrderCount()));
                    vo.setOrderCount(Integer.parseInt(scoreDetailVO.getOrderCount()));
                } else {
                    vo.setScore(0);
                    vo.setOrderCount(0);
                }
            }
            //根据分数降序排序
            ComparatorResultType comparator=new ComparatorResultType();
            //正常排序
            Collections.sort(voList, comparator);
            //进行降序
            Collections.reverse(voList);
        }
        return getPageData(voList, voList.size(), MasterPageVO.class);
    }

标签:MasterPageVO,List,scoreDetailVO,vo,voList,getOrderCount,排序,size
来源: https://www.cnblogs.com/xiaoguo-java/p/15988948.html