Comparator使用记录

1、实体类

@Entity
@Table(name = "game_server_pay")
public class EntityGameServerPay implements Serializable{
	private static final long serialVersionUID = 2L;

    @Id
    private String projectname;
    @Column(columnDefinition="int(11) NOT NULL DEFAULT '0'")
    private int projectid;
    @Id
    private String month;
    @Id
    private String supplier;
    @Column(columnDefinition="int(11) NOT NULL DEFAULT '0'")
    private int servercount;
    @Column(columnDefinition="double(16,2) NOT NULL DEFAULT '0.00'")
    private Double serverpay;
    @Column(columnDefinition="double(16,2) NOT NULL DEFAULT '0.00'")
    private Double bandwidthflow;
    @Column(columnDefinition="double(16,2) NOT NULL DEFAULT '0.00'")
    private Double bandwidthpay;
    @Column(columnDefinition="double(16,2) NOT NULL DEFAULT '0.00'")
    private Double cdnuse;
    @Column(columnDefinition="double(16,2) NOT NULL DEFAULT '0.00'")
    private Double monthcdnpay;
    @Column(columnDefinition="double(16,2) NOT NULL DEFAULT '0.00'")
    private Double monthpay;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getProjectname() {
        return projectname;
    }

    public void setProjectname(String projectname) {
        this.projectname = projectname;
    }

    public int getProjectid() {
        return projectid;
    }

    public void setProjectid(int projectid) {
        this.projectid = projectid;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getSupplier() {
        return supplier;
    }

    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }

    public int getServercount() {
        return servercount;
    }

    public void setServercount(int servercount) {
        this.servercount = servercount;
    }

    public Double getServerpay() {
        return serverpay;
    }

    public void setServerpay(Double serverpay) {
        this.serverpay = serverpay;
    }

    public Double getBandwidthflow() {
        return bandwidthflow;
    }

    public void setBandwidthflow(Double bandwidthflow) {
        this.bandwidthflow = bandwidthflow;
    }

    public Double getBandwidthpay() {
        return bandwidthpay;
    }

    public void setBandwidthpay(Double bandwidthpay) {
        this.bandwidthpay = bandwidthpay;
    }

    public Double getCdnuse() {
        return cdnuse;
    }

    public void setCdnuse(Double cdnuse) {
        this.cdnuse = cdnuse;
    }

    public Double getMonthcdnpay() {
        return monthcdnpay;
    }

    public void setMonthcdnpay(Double monthcdnpay) {
        this.monthcdnpay = monthcdnpay;
    }

    public Double getMonthpay() {
        return monthpay;
    }

    public void setMonthpay(Double monthpay) {
        this.monthpay = monthpay;
    }
}

2、排序的逻辑

     1、按照month、projectname、supplier的顺序排序

     2、projectname、supplier按照汉语的拼音顺序排序

public class Utils {
   //根据汉语的顺序排序
private final static Comparator<Object> CHINA_COMPARE = Collator.getInstance(Locale.CHINESE);

public final Comparator<EntityGameServerPay> comparatorMonthAndProjectnameAndSupplier = new Comparator<EntityGameServerPay>() {
        public int compare(EntityGameServerPay s1, EntityGameServerPay s2) {
            if (s1.getMonth()!=null && s2.getMonth()!=null && !s1.getMonth().equals(s2.getMonth())) {
                return s2.getMonth().compareTo(s1.getMonth());
            }else if (s1.getProjectname()!=null && s2.getProjectname()!=null && !s1.getProjectname().equals(s2.getProjectname())) {
                return CHINA_COMPARE.compare(s1.getProjectname(),s2.getProjectname());
            }else if (s1.getSupplier() !=null && s2.getSupplier() !=null && !s1.getSupplier().equals(s2.getSupplier())){
                return CHINA_COMPARE.compare(s1.getSupplier(),s2.getSupplier());
            }
            return 0;
        }
    };
}

3、使用排序逻辑

//获取数据
        List<EntityGameServerPay> data = gameServerPayManager.getGameServerPay(start, end, month, getParams(projectnameArry), getParams(supplierArry));
        //数据排序
        Collections.sort(data, new Utils().comparatorMonthAndProjectnameAndSupplier);

猜你喜欢

转载自blog.csdn.net/Poppy_Evan/article/details/102600839