java中集合类自定义排序 可以实现升序降序

首先定义一个需要排序类 这里实现 自定义类 首个属性降序 其他升序

这个自定义类 不用实现什么接口 我这里是另做他用


package cn.tukk.sparkproject.spark.session;

import scala.Serializable;
import scala.math.Ordered;

public class MyCategorySortKey implements Ordered<MyCategorySortKey>  , Serializable {

    private static final long serialVersionUID = 197033426188166691L;

    private int clickCount;
    private int orderCount;
    private int payCount;

    public MyCategorySortKey(int clickCount, int orderCount, int payCount) {
        this.clickCount = clickCount;
        this.orderCount = orderCount;
        this.payCount = payCount;
    }

    @Override
    public int compare(MyCategorySortKey that) {
        if(clickCount-that.getClickCount()!=0){
            return (int) (clickCount-that.getClickCount());
        }else if(orderCount-that.getOrderCount()!=0){
            return (int) (clickCount-that.getOrderCount());
        }else if(payCount-that.getPayCount()!=0){
            return (int) (payCount-that.getPayCount());
        }
        return 0;
    }

    @Override
    public boolean $greater(MyCategorySortKey other) {
        if(clickCount > other.getClickCount()) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount > other.getOrderCount()) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount == other.getOrderCount() &&
                payCount > other.getPayCount()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean $greater$eq(MyCategorySortKey other) {
        if($greater(other)) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount == other.getOrderCount() &&
                payCount == other.getPayCount()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean $less(MyCategorySortKey other) {
        if(clickCount < other.getClickCount()) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount < other.getOrderCount()) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount == other.getOrderCount() &&
                payCount < other.getPayCount()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean $less$eq(MyCategorySortKey other) {
        if($less(other)) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount == other.getOrderCount() &&
                payCount == other.getPayCount()) {
            return true;
        }
        return false;
    }


    @Override
    public int compareTo(MyCategorySortKey that) {
        if(clickCount-that.getClickCount()!=0){
            return (int) (clickCount-that.getClickCount());
        }else if(orderCount-that.getOrderCount()!=0){
            return (int) (clickCount-that.getOrderCount());
        }else if(payCount-that.getPayCount()!=0){
            return (int) (payCount-that.getPayCount());
        }
        return 0;
    }



    public int getClickCount() {
        return clickCount;
    }

    public void setClickCount(int clickCount) {
        this.clickCount = clickCount;
    }

    public int getOrderCount() {
        return orderCount;
    }

    public void setOrderCount(int orderCount) {
        this.orderCount = orderCount;
    }

    public int getPayCount() {
        return payCount;
    }

    public void setPayCount(int payCount) {
        this.payCount = payCount;
    }

    @Override
    public String toString() {
        return "MyCategorySortKey{" +
                "clickCount=" + clickCount +
                ", orderCount=" + orderCount +
                ", payCount=" + payCount +
                '}';
    }
}

其次编写排序测试

package cn.tukk.sparkproject.test.testOrder;

import cn.tukk.sparkproject.spark.session.MyCategorySortKey;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*
 */
public class Myorder {
    public static void main(String[] args) {

        MyCategorySortKey my1=new MyCategorySortKey(12,15,20);
        MyCategorySortKey my2=new MyCategorySortKey(15,15,20);
        MyCategorySortKey my3=new MyCategorySortKey(12,15,16);
        MyCategorySortKey my4=new MyCategorySortKey(12,17,16);
        List<MyCategorySortKey> list=new ArrayList<MyCategorySortKey>();
        list.add(my1);
        list.add(my2);
        list.add(my3);
        list.add(my4);

        // 按点击数倒序
        Collections.sort(list, new Comparator<MyCategorySortKey>() {
            public int compare(MyCategorySortKey arg0, MyCategorySortKey arg1) {
                if(arg0.getClickCount()-arg1.getClickCount()!=0){
                    if(arg0.getClickCount()-arg1.getClickCount()>0){//倒序
                        return  -1;
                    }if(arg0.getClickCount()-arg1.getClickCount()<0){
                        return 1;
                    }else {
                        return 0;
                    }

                }else if(arg0.getOrderCount()-arg1.getOrderCount()!=0){
                    return (int) (arg0.getOrderCount()-arg1.getOrderCount());
                }else if(arg0.getPayCount()-arg1.getPayCount()!=0){
                    return (int) (arg0.getPayCount()-arg1.getPayCount());
                }
               return 0;
            }
        });

        for (MyCategorySortKey my:list
             ) {
            System.out.println(my);
        }
    }

}

输出结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值