list集合排序

本文详细介绍了Java中使用Collections.sort方法对List集合进行排序的过程。包括基本类型和自定义类型的排序方式,通过实现Comparable接口和使用Comparator接口两种方法,并提供了具体的代码实例。

https:/blog.youkuaiyun.com/veryisjava/article/details/51675036

 1 public static void main(String[] args) {  
 2     List<Integer> nums = new ArrayList<Integer>();  
 3         nums.add(3);  
 4         nums.add(5);  
 5         nums.add(1);  
 6         nums.add(0);  
 7         System.out.println(nums);  
 8         Collections.sort(nums);  
 9         System.out.println(nums);  
10 }  
test1

输出结果:
[3, 5, 1, 0]
[0, 1, 3, 5]

 1 package core.java.collection.collections;  
 2   
 3 public class User implements Comparable<User>{  
 4       
 5     private int score;  
 6       
 7     private int age;  
 8       
 9     public User(int score, int age){  
10         super();  
11         this.score = score;  
12         this.age = age;  
13     }  
14   
15     public int getScore() {  
16         return score;  
17     }  
18   
19     public void setScore(int score) {  
20         this.score = score;  
21     }  
22   
23     public int getAge() {  
24         return age;  
25     }  
26   
27     public void setAge(int age) {  
28         this.age = age;  
29     }  
30   
31     @Override  
32     public int compareTo(User o) {  
33         int i = this.getAge() - o.getAge();//先按照年龄排序  
34         if(i == 0){  
35             return this.score - o.getScore();//如果年龄相等了再用分数进行排序  
36         }  
37         return i;  
38     }  
39       
40 }  
41   
42 public static void main(String[] args) {  
43         List<User> users = new ArrayList<User>();  
44         users.add(new User(78, 26));  
45         users.add(new User(67, 23));  
46         users.add(new User(34, 56));  
47         users.add(new User(55, 23));  
48         Collections.sort(users);  
49         for(User user : users){  
50             System.out.println(user.getScore() + "," + user.getAge());  
51         }  
52 }  
test2

输出结果:
55,23
67,23
78,26
34,56

=======================

Collections提供的第二种排序方法sort(List<T> list, Comparator<? super T> c)

 1 package core.java.collection.collections;  
 2   
 3 public class Students {  
 4       
 5     private int age;  
 6     private int score;  
 7       
 8     public Students(int age, int score){  
 9         super();  
10         this.age = age;  
11         this.score = score;  
12     }  
13       
14     public int getAge() {  
15         return age;  
16     }  
17     public void setAge(int age) {  
18         this.age = age;  
19     }  
20     public int getScore() {  
21         return score;  
22     }  
23     public void setScore(int score) {  
24         this.score = score;  
25     }  
26 }  
27 public static void main(String[] args) {  
28         List<Students> students = new ArrayList<Students>();  
29         students.add(new Students(23, 100));  
30         students.add(new Students(27, 98));  
31         students.add(new Students(29, 99));  
32         students.add(new Students(29, 98));  
33         students.add(new Students(22, 89));  
34         Collections.sort(students, new Comparator<Students>() {  
35   
36             @Override  
37             public int compare(Students o1, Students o2) {  
38                 int i = o1.getScore() - o2.getScore();  
39                 if(i == 0){  
40                     return o1.getAge() - o2.getAge();  
41                 }  
42                 return i;  
43             }  
44         });  
45         for(Students stu : students){  
46             System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());  
47         }  
48 }  
test3

输出结果:
score:89:age22
score:98:age27
score:98:age29
score:99:age29
score:100:age23

从上面的例子我们可以看出Students类没有实现Comparable<T>接口,只是在sort()方法
中多传入一个参数,只不过该参数是一个接口我们需要实现其compare方法。

转载于:https://www.cnblogs.com/lljboke/p/8868356.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值