码迷,mamicode.com
首页 > 编程语言 > 详细

集合的排序(正序,倒序,从大到小排序)

时间:2018-06-04 16:52:18      阅读:1666      评论:0      收藏:0      [点我收藏+]

标签:排序   alibaba   tag   turn   string   pen   spl   pac   opened   

1.简单的集合排序

技术分享图片
 1 package com.demo.advice;
 2 
 3 import java.util.Collections;
 4 import java.util.LinkedList;
 5 
 6 import com.alibaba.druid.sql.visitor.functions.Reverse;
 7 
 8 public class Test {
 9     public static void main(String[] args) {
10         
11         LinkedList<Integer> list = new LinkedList<Integer>();
12         list.add(1);
13         list.add(8);
14         list.add(7);
15         list.add(4);
16         list.add(5);
17         //排序前
18         System.out.println(list);
19         //倒序
20         Collections.reverse(list);
21         System.out.println(list);
22         //从大到小排序
23         Collections.sort(list);
24         System.out.println(list);
25         
26     }
27     
28 }
View Code

排序结果图

技术分享图片

2.复杂一点的集合排序

 1 package com.demo.advice;
 2 
 3 
 4 import java.util.Collections;
 5 import java.util.LinkedList;
 6 
 7 
 8 
 9 public class Test {
10     public static void main(String[] args) {
11         LinkedList<Users> list = new LinkedList<Users>();
12         list.add(new Users(1,"aa",20));
13         list.add(new Users(4,"bb",30));
14         list.add(new Users(3,"cc",25));
15         list.add(new Users(3,"dd",42));
16         list.add(new Users(8,"ee",35));
17         Collections.sort(list);
18         for (Users users : list) {
19             System.out.println(users);
20         }
21     }
22     
23 }

技术分享图片

下面是实体类实现comparable接口

技术分享图片
 1 package com.demo.advice;
 2 
 3 public class Users implements Comparable<Users> {
 4 
 5     private int id;
 6     private String name;
 7     private int age;
 8     public Users() {
 9         super();
10         // TODO Auto-generated constructor stub
11     }
12     public Users(Integer id, String name, Integer age) {
13         super();
14         this.id = id;
15         this.name = name;
16         this.age = age;
17     }
18     @Override
19     public String toString() {
20         return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
21     }
22     
23     
24     public int getId() {
25         return id;
26     }
27     public void setId(int id) {
28         this.id = id;
29     }
30     public String getName() {
31         return name;
32     }
33     public void setName(String name) {
34         this.name = name;
35     }
36     public int getAge() {
37         return age;
38     }
39     public void setAge(int age) {
40         this.age = age;
41     }
42     @Override
43     public int compareTo(Users o) {
44         int i = this.getId() - o.getId();
45         if (i==0) {
46             return this.getAge() - o.getAge();
47         }
48         return i;
49         // TODO Auto-generated method stub
50         
51     }
52 }
View Code

 

集合的排序(正序,倒序,从大到小排序)

标签:排序   alibaba   tag   turn   string   pen   spl   pac   opened   

原文地址:https://www.cnblogs.com/xaoco/p/9133377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!