List<Obj>按obj排序

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

public class Testa {

    public static void main(String[] args) {
        
        List<Obj> l = new ArrayList<>();
        Obj obj1 = new Obj(1,"一");
        Obj obj2 = new Obj(2,"二");
        Obj obj3 = new Obj(3,"三");
        l.add(obj1);
        l.add(obj3);
        l.add(obj2);
        
        
        Collections.sort(l, new Comparator<Obj>() {

            public int compare(Obj o1, Obj o2) {

                // 升序
                if (o1.getA() > o2.getA()) {

                    return 1;
                }
                if (o1.getA() == o2.getA()) {

                    return 0;
                }
                return -1;
            }
        });
        
        for (int i = 0; i < l.size(); i++) {
            
            System.out.println(l.get(i).toString());
        }
    }

}

class Obj {

    int a;

    String s;
    
    public Obj() {}

    public Obj(int a, String s) {
        super();
        this.a = a;
        this.s = s;
    }

    @Override
    public String toString() {
        return "Obj [a=" + a + ", s=" + s + "]";
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

}
这个博客主要是javaEE相关或者不相关的记录, hadoop与spark的相关文章我写在下面地址的博客啦~ http://www.cnblogs.com/sorco
原文地址:https://www.cnblogs.com/orco/p/6524221.html