Java中如何实现类似C++结构体的二级排序

1:实现Comparable接口

 

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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
 
class User implements Comparable<User>{
    private Integer id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public int compareTo(User o) {
        if (this.id == o.id) return this.name.compareTo(o.name);  //id相同按name排序
        else return this.id.compareTo(o.id); //按ID从小到大排序
    }
}
 
public class Main {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         
         
        String s1 = "abx";
        String s2 = "小1";
        System.out.println(s1.compareTo(s2));
         
         
        List<User> users = new ArrayList<User>();
         
        User u6 = new User();
        u6.setId(6); u6.setName("Jack");
         
         
        User u = new User();
        u.setId(6); u.setName("abx");
         
         
        User u1 = new User();
        u1.setId(1); u1.setName("小1");
         
         
        User u4 = new User();
        u4.setId(4); u4.setName("小4");
         
         
        User u2 = new User();
        u2.setId(2); u2.setName("小2");
         
        User u7 = new User();
        u7.setId(6); u7.setName("abc");
         
        users.add(u2);
        users.add(u6);
        users.add(u4);
        users.add(u1);
        users.add(u);
        users.add(u7);
        Collections.sort(users);
         
        for (User ui : users) {
            System.out.println(ui.getId() + " " + ui.getName());
        }
    }
 
}

  

2:集合外定义Comparator

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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
 
class User{
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
 
public class Main {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         
         
        String s1 = "abx";
        String s2 = "小1";
        System.out.println(s1.compareTo(s2));
         
         
        List<User> users = new ArrayList<User>();
         
        User u6 = new User();
        u6.setId(6); u6.setName("Jack");
         
         
        User u = new User();
        u.setId(6); u.setName("abx");
         
         
        User u1 = new User();
        u1.setId(1); u1.setName("小1");
         
         
        User u4 = new User();
        u4.setId(4); u4.setName("小4");
         
         
        User u2 = new User();
        u2.setId(2); u2.setName("小2");
         
        User u7 = new User();
        u7.setId(6); u7.setName("abc");
         
        users.add(u2);
        users.add(u6);
        users.add(u4);
        users.add(u1);
        users.add(u);
        users.add(u7);
         
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                if (o1.getId() == o2.getId()) return o1.getName().compareTo(o2.getName());
                return o1.getId() > o2.getId() ? 1 : -1;
            }
             
        });
         
        for (User ui : users) {
            System.out.println(ui.getId() + " " + ui.getName());
        }
    }
 
}

  

 

 

posted @   E_star  阅读(567)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示