java list Comparator排序

  • bean结构
    implements Comparator
	public  class PubMsgClassAuditReportItem implements Comparator {
	
	    private String id;
	    private String userId;
	    private String userName;
	    private int edit_type;
	    private String msg_id;//资讯id'
	    private int newScore;//新增得分
	    private int updateScore;
	    private int deleteScore;
	    private Date createTime;
	    private int newNum;
	    private int updateNum;
	    private int deleteNum;
	    private int newNum5;//新增审核不通过
	    private int updateNum5;
	    private int deleteNum5;
	    private int totalScore;
	
        。。。。。。
	
	     //重写方法
	    @Override
	    public int compare(PubMsgClassAuditReportItem item1, PubMsgClassAuditReportItem item2) {
	        if (item1.getTotalScore() > item2.getTotalScore()) {
	            return -1; //降序  return 1;升序
	        } else if (item1.getTotalScore() == item2.getTotalScore()) {
	            return 0;
	        } else {
	            return 1;
	        }
	    }
	}

  • 方法使用
    Collections.sort(list, new PubMsgClassAuditReportItem());//此时list已经排序,直接可以使用

  • Collections.sort(list,(PubMsgClassAuditReportItem item1,PubMsgClassAuditReportItem item2) -> item1.getTotalScore() - item2.getTotalScore());//此方法需要level>6

  • 其他方法

你可能感兴趣的:(Java)