forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortLambda.java
More file actions
43 lines (38 loc) · 1.1 KB
/
SortLambda.java
File metadata and controls
43 lines (38 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package JavaBasic;
import java.util.ArrayList;
import java.util.Collections;
/**
* @Classname SortLambda
* @Description 使用lambda表达式重写排序方法
* @Date 19-7-7 下午3:17
* @Created by mao<tianmao818@qq.com>
*/
class People{
private String name;
private int age;
public People(String name,int age){
this.name=name;
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
public class SortLambda {
public static void main(String[] args){
ArrayList<People> arrayList=new ArrayList<>();
arrayList.add(new People("test1",25));
arrayList.add(new People("test3",24));
arrayList.add(new People("test2",27));
//按照姓名逆字典排序
Collections.sort(arrayList,(a,b)->b.getName().compareTo(a.getName()));
// //按照年龄顺序排序
// Collections.sort(arrayList,(a,b)->a.getName().compareTo(b.getName()));
for(People people:arrayList){
System.out.println(people.getName()+","+people.getAge());
}
}
}