class Person
{
private String name;
private String profession;
}
profession has values:
- engineer
- Doctor<
ComparableYes you can implement Comparable. Create a method compareTo(Person obj) and then write your custom logic. You can compare alphabetically or whatever other algorithm you want - for example engineer before doctor because he makes more money :) For alphabetic comparing you can do it like that:
class Person implements Comparable {
@Override
public int compareTo(Person o) {
return this.profession.compareTo(o.getProfession());
}
private String name;
private String profession;
}
After that you just use the Collections.sort