【Java】对象集合排序

小结:

  使用比较器时,返回值必须是int类型, 如果要比较的内容,返回值不是int,则需要根据 >0  =0  <0 来分别写return值

  如果返回值是int 类型,则直接返回即可,这里的写法是 按倒叙排列,如果要升序,把b和a的顺序调换一下即可

//使用Comparator比较器对集合排序
        Comparator<MyStoreObject> oop=new Comparator<MyStoreObject>() {
            public int compare(MyStoreObject o1, MyStoreObject o2) {
                double a = o1.getDifference();
                double b = o2.getDifference();
          double temp = b-a;
if(temp>0) return 1; else if(temp==0) return 0; else return -1; } };

 

 

 

代码如下:

public class objectStore {
    public static void main(String[] args) {
        //Store();
        myStore();
    }

    private static void Store(){
        List<Pointer> list=new ArrayList<Pointer>();
        list.add(new Pointer(2,3));
        list.add(new Pointer(1,4));
        list.add(new Pointer(0,5));
        list.add(new Pointer(8,9));
        list.add(new Pointer(7,6));
        list.add(new Pointer(1,5));
        list.add(new Pointer(6,8));

        //使用Comparator比较器对集合排序
        Comparator<Pointer> oop=new Comparator<Pointer>() {
            public int compare(Pointer o1, Pointer o2) {
                int a=o1.getX()+o1.getY();
                int b=o2.getX()+o2.getY();
                return b-a;
            }
        };
        Collections.sort(list, oop);
        for (Pointer pointer : list) {
            System.out.println(pointer);
        }

        //原文链接:https://blog.csdn.net/weixin_47600732/article/details/115059110
    }

    private static void myStore(){
        List<MyStoreObject> list=new ArrayList<MyStoreObject>();
        list.add(new MyStoreObject("1",20.0,10,30));
        list.add(new MyStoreObject("2",10.0,10,30));
        list.add(new MyStoreObject("3",25.0,10,30));
        list.add(new MyStoreObject("4",27.0,10,30));
        list.add(new MyStoreObject("5",0.0,10,30));
        list.add(new MyStoreObject("6",-10.0,10,30));

        //使用Comparator比较器对集合排序
        Comparator<MyStoreObject> oop=new Comparator<MyStoreObject>() {
            public int compare(MyStoreObject o1, MyStoreObject o2) {
                double a = o1.getDifference();
                double b = o2.getDifference();
                if((b-a)>0)
                    return 1;
                else if((b-a)==0)
                    return 0;
                else
                    return -1;
            }
        };
        Collections.sort(list, oop);
        for (MyStoreObject myStoreObject : list) {
            System.out.println(myStoreObject);
        }
    }
}
View Code

 

实体类代码如下:

public class MyStoreObject {
    private String stationId;

    private double difference;

    private double lastWeekTotalChargedAmount;

    private double theWeekBeforeLastTotalChargedAmount;

    public MyStoreObject(String stationId, double difference, double lastWeekTotalChargedAmount, double theWeekBeforeLastTotalChargedAmount) {
        this.stationId = stationId;
        this.difference = difference;
        this.lastWeekTotalChargedAmount = lastWeekTotalChargedAmount;
        this.theWeekBeforeLastTotalChargedAmount = theWeekBeforeLastTotalChargedAmount;
    }

    public String getStationId() {
        return stationId;
    }

    public void setStationId(String stationId) {
        this.stationId = stationId;
    }

    public double getDifference() {
        return difference;
    }

    public void setDifference(double difference) {
        this.difference = difference;
    }

    public double getLastWeekTotalChargedAmount() {
        return lastWeekTotalChargedAmount;
    }

    public void setLastWeekTotalChargedAmount(double lastWeekTotalChargedAmount) {
        this.lastWeekTotalChargedAmount = lastWeekTotalChargedAmount;
    }

    public double getTheWeekBeforeLastTotalChargedAmount() {
        return theWeekBeforeLastTotalChargedAmount;
    }

    public void setTheWeekBeforeLastTotalChargedAmount(double theWeekBeforeLastTotalChargedAmount) {
        this.theWeekBeforeLastTotalChargedAmount = theWeekBeforeLastTotalChargedAmount;
    }

    @Override
    public String toString() {
        return "MyStoreObject{" +
                "stationId='" + stationId + '\'' +
                ", difference=" + difference +
                ", lastWeekTotalChargedAmount=" + lastWeekTotalChargedAmount +
                ", theWeekBeforeLastTotalChargedAmount=" + theWeekBeforeLastTotalChargedAmount +
                '}';
    }
}
View Code

 

public class Pointer {
    private int x;
    private  int y;

    public Pointer(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Pointer{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}
View Code

 

参考自:https://blog.csdn.net/weixin_47600732/article/details/115059110

posted @ 2022-05-09 11:44  狼窝窝  阅读(72)  评论(0)    收藏  举报