Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person {
- private final String firstName;
- private final int age;
- public Person(final String firstName, final int age) {
- this.firstName = firstName;
- this.age = age;
- }
- @Override
- public String toString() {
- return "Person{" + "firstName='" + this.firstName + '\'' + ", age=" + this.age + '}';
- }
- public enum Comparators implements Comparator<Person> {
- BY_FIRST_NAME {
- @Override
- public int compare(final Person o1, final Person o2) {
- return o1.firstName.compareTo(o2.firstName);
- }
- },
- BY_AGE {
- @Override
- public int compare(final Person o1, final Person o2) {
- return Integer.compare(o1.age, o2.age);
- }
- }
- }
- public static void main(final String[] args) {
- final List<Person> persons = new ArrayList<>();
- persons.add(new Person("jim", 3));
- persons.add(new Person("bob", 5));
- persons.sort(Comparators.BY_FIRST_NAME);
- System.out.println(persons);
- persons.sort(Comparators.BY_AGE);
- System.out.println(persons);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement