-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathArray12.java
More file actions
70 lines (51 loc) · 1.11 KB
/
Array12.java
File metadata and controls
70 lines (51 loc) · 1.11 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
package Day3;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class hello implements Comparable<hello> {
private int id;
private String name;
public hello(int id, String name) {
super();
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;
}
@Override
public int compareTo(hello o) {
return id - o.getId();
}
}
class compareName implements Comparator<hello> {
@Override
public int compare(hello h1, hello h2) {
return h1.getName().compareTo(h2.getName());
}
}
public class Array12 {
public static void main(String[] args) {
ArrayList<hello> arr = new ArrayList<hello>();
hello h1 = new hello(1, "Ismail");
hello h2 = new hello(3, "Mohammed");
hello h3 = new hello(2, "Kumar");
arr.add(h1);
arr.add(h2);
arr.add(h3);
// Collections.sort(arr, new compareName());
Collections.sort(arr);
for (hello h : arr) {
System.out.println(h.getId() + " " + h.getName());
}
}
}