服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - List集合对象中按照不同属性大小排序的实例

List集合对象中按照不同属性大小排序的实例

2020-08-24 10:59Java教程网 Java教程

下面小编就为大家带来一篇List集合对象中按照不同属性大小排序的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

实例如下:

?
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.huad.luck;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
public class Test {
    public static void main(String[] args) {
        Person p = new Person();
        p.setName("tom");
        p.setAge(11);
        Person p1 = new Person();
        p1.setName("lilei");
        p1.setAge(19);
        Person p2 = new Person();
        p2.setName("json");
        p2.setAge(15);
        List<Person> list =new ArrayList<Person>();
        list.add(p);
        list.add(p1);
        list.add(p2);
        Collections.sort(list,new Comparator<Person>() {
            //这里可以再Person中实现 Comparator<T>接口,重写compare方法
            @Override
            public int compare(Person o1, Person o2) {
                //这里按照名字排序
                 return o1.getName().compareTo(o2.getName());
                 //这里按照age排序
                 //return (o1.getAge()+"").compareTo(o2.getAge()+"");
                 //。。。根据不同属性值排序
            
 
        });
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getName());
        }
    }
}
//Person类
package com.huad.luck;
 
public class Person{
    private String name ;
    private int age;
    
    public Person(String name , int age) {
        this.name = name;
        this.age = age;
    }
    public Person( ) {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
 
}
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 8px; margin-bottom: 8px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 12.5px; word-wrap: break-word; word-break: break-all; color: rgb(51, 51, 51); font-family: 'Microsoft YaHei', Verdana, sans-serif, 宋体;"><span style="padding: 0px; margin: 0px; letter-spacing: normal; font-stretch: normal; font-size: 14px; line-height: 24px; font-family: Tahoma; color: rgb(0, 0, 0);">用collection.sort()方法对list集合排序</span><br style="padding: 0px; margin: 0px;" /></p><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 24px; font-size: 14px; word-wrap: break-word; word-break: break-all; font-stretch: normal; font-family: Tahoma;"><span style="padding: 0px; margin: 0px; list-style: none outside none; word-wrap: break-word; word-break: normal;"><span style="padding: 0px; margin: 0px; list-style: none outside none; line-height: 21px; word-wrap: break-word; word-break: normal;"></span></span></p><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 1em; margin-bottom: 0.5em; line-height: 24px; font-size: 14px; word-wrap: break-word; word-break: break-all; font-stretch: normal; font-family: Tahoma;">第一种是list中的对象实现Comparable接口,如下:<span style="padding: 0px; margin: 0px; list-style: none outside none; word-wrap: break-word; word-break: normal;"> </span></p><pre class="java" style="padding: 5px; margin-top: 0px; margin-bottom: 0px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border: 1px solid rgb(221, 221, 221); color: rgb(51, 51, 51); background: rgb(246, 246, 246);">/**
* 根据order对User排序
*/
public class User implements Comparable<User>{
  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;
  }
  public int compareTo(User arg0) {
    return this.getOrder().compareTo(arg0.getOrder());
  }
}

测试一下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Test{
  public static void main(String[] args) {
    User user1 = new User();
    user1.setName("a");
    user1.setOrder(1);
    User user2 = new User();
    user2.setName("b");
    user2.setOrder(2);
    List<User> list = new ArrayList<User>();
    //此处add user2再add user1
    list.add(user2);
    list.add(user1);
    Collections.sort(list);
    for(User u : list){
      System.out.println(u.getName());
    }
  }
}

输出结果如下

a
b

第二种方法是根据Collections.sort重载方法来实现,例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 根据order对User排序
*/
public class User { //此处无需实现Comparable接口
  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;
  }
}

主类中这样写即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Test{
  public static void main(String[] args) {
    User user1 = new User();
    user1.setName("a");
    user1.setOrder(1);
    User user2 = new User();
    user2.setName("b");
    user2.setOrder(2);
    List<User> list = new ArrayList<User>();
    list.add(user2);
    list.add(user1);
    
    Collections.sort(list,new Comparator<User>(){
      public int compare(User arg0, User arg1) {
        return arg0.getOrder().compareTo(arg1.getOrder());
      }
    });
    for(User u : list){
      System.out.println(u.getName());
    }
  }
}

输出结果如下

a
b

前者代码结构简单,但是只能根据固定的属性排序,后者灵活,可以临时指定排序项,但是代码不够简洁

多字段的场合:

?
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
Collections.sort(list,new Comparator<User>(){ 
 
public int compare(User arg0, User arg1) { 
 
// 第一次比较专业 
 
int i = arg0.getOrder().compareTo(arg1.getOrder());
 
// 如果专业相同则进行第二次比较 
 
if(i==0){ 
 
// 第二次比较 
 
int j=arg0.getXXX().compareTo(arg1.getXXX()); 
 
// 如果学制相同则返回按年龄排序 
 
if(j==0){ 
 
return arg0.getCCC().compareTo(arg1.getCCC()); 
 
 
return j; 
 
 
return i; 
 
 
});

以下是另外一个例子

?
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package test;
 
import java.util.ArrayList; 
 
import java.util.Collections; 
 
import java.util.Comparator;
 
public class ArrayListTest { 
 
public static void main(String[] args) { 
 
ArrayList<Exmployee> arrayList = new ArrayList<Exmployee>() ; 
 
arrayList.add(new Exmployee("zengqiang",new Integer(5000))) ; 
 
arrayList.add(new Exmployee("zengmin",new Integer(4000))) ; 
 
arrayList.add(new Exmployee("liuxiaojuan",new Integer(4200))) ; 
 
arrayList.add(new Exmployee("giuming",new Integer(2200))) ; 
 
Collections.sort(arrayList, new Comparator<Exmployee>(){ 
 
public int compare(Exmployee arg0, Exmployee arg1) { 
 
return arg1.getSalary()-arg0.getSalary() ; //按照工资升序 
 
//return arg0.getSalary()-arg1.getSalary() ; 按照工资降序 
 
//return arg0.getName().compareTo(arg1.getName()) ; 按照姓名升序 
 
 
}); 
 
for(Exmployee e:arrayList) 
 
System.out.println(e.getName()+"'s salary is "+e.getSalary()) ; 
 
}
 
}
 
class Exmployee { 
 
public Exmployee(String name, int salary) { 
 
this.name = name ; 
 
this.salary = salary ; 
 
}
 
private String name;
 
public String getName() { 
 
return name; 
 
}
 
public void setName(String name) { 
 
this.name = name; 
 
}
 
public int getSalary() { 
 
return salary; 
 
}
 
public void setSalary(int salary) { 
 
this.salary = salary; 
 
}
 
private int salary; 
 
}

以上这篇List集合对象中按照不同属性大小排序的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐