public class Employee implements Comparable
1: //Employee.java
3: public class Employee implements Comparable<Employee>
4: {
5: private String name;
6: private int id;
7: private int age;
9: public Employee
10: (
11: String name,
12: int id,
13: int age
14: )
15: {
16: this.name = name;
17: this.id = id;
18: this.age = age;
19: }
21: public String getName()
22: {
23: return name;
24: }
26: public int getId()
27: {
28: return id;
29: }
31: public int getAge()
32: {
33: return age;
34: }
36: public String toString()
37: {
38: return name + "\t" + id + "\t" + age;
39: }
41: //To sort by age
42: public int compareTo(Employee other)
43: {
44: return age - other.age;
45: }
46: }
47: /*
48: //To sort by name
49: public int compareTo(Employee other)
50: {
51: return name.compareTo(other.name);
52: }
54: //To sort by id
55: public int compareTo(Employee other)
56: {
57: return id - other.id;
58: }
59: */
60: /*
61: Note:
62: Since Employee implements Comparable<Employee>
63: it must provide a definition for the compareTo()
64: method. But it can only provide one implementation,
65: so we must choose to compare names, ids, or ages,
66: since we can only make one kind of comparison in
67: this way. At the moment we are comparing ages.
68: */