-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path7. Sorting Lists Using Comparators
More file actions
146 lines (110 loc) · 3.69 KB
/
7. Sorting Lists Using Comparators
File metadata and controls
146 lines (110 loc) · 3.69 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id;
this.name = 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 String toString() {
return id + ": " + name;
}
}
class StringLengthComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
int len1 = s1.length();
int len2 = s2.length();
if(len1 > len2) {
return 1;
}
else if(len1 < len2) {
return -1;
}
return 0;
}
}
class ReverseAlphabeticalComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return -s1.compareTo(s2);
}
}
public class App {
public static void main(String[] args) {
////////////////////// Sorting Strings ////////////////////////////////
List<String> animals = new ArrayList<String>();
animals.add("tiger");
animals.add("lion");
animals.add("cat");
animals.add("snake");
animals.add("mongoose");
animals.add("elephant");
// Collections.sort(animals, new StringLengthComparator());
Collections.sort(animals, new ReverseAlphabeticalComparator());
for(String animal: animals) {
System.out.println(animal);
}
////////////////////// Sorting Numbers ////////////////////////////////
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(3);
numbers.add(36);
numbers.add(73);
numbers.add(40);
numbers.add(1);
Collections.sort(numbers, new Comparator<Integer>() {
public int compare(Integer num1, Integer num2) {
return -num1.compareTo(num2);
}
});
for(Integer number: numbers) {
System.out.println(number);
}
////////////////////// Sorting arbitary objects ////////////////////////////////
List<Person> people = new ArrayList<Person>();
people.add(new Person(1, "Joe"));
people.add(new Person(3, "Bob"));
people.add(new Person(4, "Clare"));
people.add(new Person(2, "Sue"));
// Sort in order of ID
Collections.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
if(p1.getId() > p2.getId()) {
return 1;
}
else if(p1.getId() < p2.getId()) {
return -1;
}
return 0;
}
});
for(Person person: people) {
System.out.println(person);
}
System.out.println("n");
// Sort in order of name
Collections.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
});
for(Person person: people) {
System.out.println(person);
}
}
}