-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
168 lines (129 loc) · 3.66 KB
/
Copy pathApp.java
File metadata and controls
168 lines (129 loc) · 3.66 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package tutorial59;
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 name;
}
}
class StringLengthComparator implements Comparator<String> {
// lets make a compare method for strings based on length of 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 ReverseAlphabeticalStringComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return -s1.compareTo(s2); // compareTo gives alphabetical order, minus
// sign reverses it
}
}
public class App {
public static void main(String[] args) {
///////////////////////// Sorting Strings //////////////////////////////
List<String> animal = new ArrayList<String>();
animal.add("tiger");
animal.add("lion");
animal.add("snake");
animal.add("mongoose");
animal.add("cat");
animal.add("elephant");
// sort method of Collections will sort list of strings in alphabetical
// order
// Collections.sort(animal);
// this sorts animals by name string length using my override method
// Collections.sort(animal, new Comparator());
Collections.sort(animal, new ReverseAlphabeticalStringComparator());
System.out.println("Reverse aphabetical String sorted animal list: ");
for (String critter : animal) {
System.out.println(critter);
}
///////////////////////// Sorting Numbers //////////////////////////////
List<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(36);
list.add(73);
list.add(40);
list.add(1);
// when given a list of integers, sort method of Collections sorts it
// naturally.. numeric
Collections.sort(list);
System.out.println("\nNatural sorted number list: ");
for (Integer number : list) {
System.out.println(number);
}
// can implement own sort as anonymous class method overriding compare
// .. example is for int reversed
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer num1, Integer num2) {
return -num1.compareTo(num2);
}
});
System.out.println("\nReverse sorted number list: ");
for (Integer number : list) {
System.out.println(number);
}
///////////////////////// Sorting Arbitrary Objects
///////////////////////// //////////////////////
List<Person> people = new ArrayList<Person>();
people.add(new Person(1, "joe"));
people.add(new Person(2, "sue"));
people.add(new Person(3, "clare"));
people.add(new Person(4, "bob"));
Collections.sort(people, new Comparator<Person>() {
// compare on higher id number
public int compare(Person p1, Person p2) {
if (p1.getId() < p2.getId()) {
return 1;
} else if (p1.getId() > p2.getId()) {
return -1;
}
return 0;
}
});
System.out.println("\nPeople sorted by id number list: ");
for (Person person : people) {
System.out.println(person);
}
Collections.sort(people, new Comparator<Person>() {
// compare on name string
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
});
System.out.println("\nPeople sorted by name list: ");
for (Person person : people) {
System.out.println(person);
}
}
}