How to sort an attribute of an object using Collections

后端 未结 6 878
眼角桃花
眼角桃花 2020-12-03 11:52

Good day!

I have an object student with the following attributes:

class Student
    String name
    Date birthday

I used arrayList

6条回答
  •  独厮守ぢ
    2020-12-03 12:10

    This can be a tricky interview question :)

    The best and reusable way i've found to solve a similar problem was to implement the interface Comparator and also creating custom Comparator according to my needs, which can be reused.

    I leave here an example how to sort an ArrayList of Person according to their name attribute and also according to their gender attribute (which don't have any lexicography natural order).

    The trick was defining a enum class with the custom attribute from which i wanted to sort. Applying a custom comparator to that enum attribute, the compareTo() method apply the order according to the natural order in which the values are declared (in this example male, female, others).

    import java.util.*;
    
    public class Person {
    
    public enum Gender {
        male,female, others
    }
    
    String name;
    int age;
    Gender gender;
    
    public Person(String name, int age, Gender gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
    
    public String getName() {
        return name;
    }
    
    public Gender getGender() {
        return gender;
    }
    
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
    
    public static void printPersons(List personList) {
    
        for (int i = 0; i < personList.size(); i++) {
            System.out.println(personList.get(i).toString());
        }
    }
    
    public static void printSortedByAgePersons(List personList) {
        Collections.sort(personList, Person.COMPARE_BY_NAME);
        for (int i = 0; i < personList.size(); i++) {
            System.out.println(personList.get(i).toString());
        }
    }
    
    public static void printSortedByGender(List personList) {
        Collections.sort(personList, Person.COMPARE_BY_GENDER);
        printPersons(personList);
    }
    
    public static Comparator COMPARE_BY_NAME = new Comparator() {
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getName().compareTo(o2.getName());
        }
    };
    
    public static Comparator COMPARE_BY_GENDER = new Comparator() {
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getGender().compareTo(o2.getGender());
        }
    };
    
    
    // lets test this :
    public static void main(String args[]) {
    
        Person p1 = new Person("André", 22, Gender.male);
        Person p2 = new Person("Minder", 19, Gender.others);
        Person p4 = new Person("Maria", 19, Gender.female);
        Person p3 = new Person("Pedro", 25, Gender.male);
    
        List personList = new ArrayList();
        personList.add(p1);
        personList.add(p2);
        personList.add(p3);
        personList.add(p4);
    
        System.out.println("original list:");
        printPersons(personList);
    
        System.out.println("------------------------------------------");
        System.out.println("sorted list by name in alphabetical order");
        printSortedByAgePersons(personList);
    
        System.out.println("------------------------------------------");
        System.out.println("sorted list by custom order(gender)");
        printSortedByGender(personList);
    
    }
    
    }
    

提交回复
热议问题