# Collections.sort方法排序，以及java8前后版本的使用

public class SortTest {
public static void main(String[] args) {
List<String> lists = new ArrayList<String>();
List<A> list = new ArrayList<A>();
List<B> listB = new ArrayList<B>();
//lists中的对象String本身含有compareTo方法，所以可以直接调用sort方法，按自然顺序排序，即升序排序
Collections.sort(lists);

A aa = new A();
aa.setName("aa");
aa.setOrder(1);
A bb = new A();
bb.setName("bb");
bb.setOrder(2);
//list中的对象A要实现Comparable接口
Collections.sort(list);

B ab = new B();
ab.setName("ab");
ab.setOrder("1");
B ba = new B();
ba.setName("ba");
ba.setOrder("2");
//listB中的对象如果没有实现Comparable接口，根据Collections.sort重载方法实现
Collections.sort(listB, new Comparable<B>()) {
@Override
public int compare(B b1, B b2) {
return b1.getOrder().compareTo(b2.getOrder);
}
};
System.out.println(lists);
System.out.println(list);
System.out.println(listB);

[2, 5, 9]
[name is aa order is 1, name is bb order is 2]
[name is ab order is 1, name is ba order is 2]

}
}

class A implements Comparable<A>{
private String name;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
@Override
public String toString() {
return "name is "+name+" order is "+order;
}
@Override
public int compareTo(A a) {
return this.order.compareTo(a.getOrder());
}

class B{
private String name;
private String order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
@Override
public String toString() {
return "name is "+name+" order is "+order;
}

Collections.sort(lists, (String a, String b)->{
return b.compareTo(a);
})

Collections.sort(lists, (String a, String b) -> b.compareTo(b));

Collections.sort(names, (a, b) -> b.compareTo(a));

#### jdk8使用lambda表达式排序

2016-08-21 14:08:54

#### JDK8中的排序

2018-04-06 09:12:01

#### JAVA8-用lamda表达式和增强版Comparator进行排序

2017-05-03 16:58:25

#### 屌炸天，JDK8的排序大法！！

2018-01-25 09:55:58

#### 原来 Java8 Stream 中的排序是插入排序

2016-08-25 16:09:58

#### java8 Arrays.sort 与Arrays.parallelSort

2016-08-28 12:02:57

#### jdk8 list排序

2017-07-12 17:42:16

#### JAVA8 十大新特性详解

2016-06-01 11:28:07

#### collections集合操作排序,Java8 stream集合操作

2018-04-04 15:57:06

#### java8collection

2016-07-01 20:16:56