Search code examples
javaarrayssortingarraylistselection-sort

How to sort parts of an ArrayList of objects of a class?


I have looked at many questions that have been asked that seem similar but none of them quite address my issue.

I have a class called Student, a class called RollManager, and a driver called RollDriver. The Student class allows a user to enter data for a student, such as their name, major, GPA, classification, etc.

The RollManager class has an ArrayList called classRoll that holds objects of type Student (looks like this: ArrayList<Student> classRoll = new ArrayList<>();

In the RollDriver is a menu that allows a user to do several things. Among them I need to be able to sort the Student objects by their names, and a separate option that allows me to sort them by their GPAs.

The issue is that when I try to use Collections.sort(classRoll), it doesn't know how to sort them. So I made a method in the RollManager class called sortName, but how do I specify that I want to sort specifically by the "name" value of the Student objects? Here's some of my code:

RollManager:

public static void sortName(ArrayList<Student> classRoll)
    {
        for(int i = 0; i < classRoll.size(); i++)
        {
            int pos = i;
            for(int n = i; n < classRoll.size(); n++)
            {
                if(classRoll.get(n).equals(classRoll.get(pos)))
                {
                    pos = n;
                }
            }
        }
    }

Option in RollDriver to sort:

else if(choice == 8)
            {
                RollManager.sortName(classRoll);
                System.out.println (classRoll);
            }

I don't get any errors when I run this, but nothing happens either. The objects aren't sorted any differently.

These are some of the pre-added objects I've added to the classRoll to test my code(the classification is an enum):

Student student2 = new Student("John", "Doe", "COMM", 120, 3.65, Classification.FRESHMAN);
Student student3 = new Student("Bob", "Ross", "ARTS", 200, 3.99, Classification.OTHER);
Student student4 = new Student("Liev", "Schreiber", "FILM", 100, 2.53, Classification.GRADUATE);
Student student5 = new Student("Maury", "Povich", "PSCI", 75, 2.24, Classification.JUNIOR);
Student student6 = new Student("Bill", "Leidermann", "CSCI", 90, 2.95, Classification.SENIOR);

classRoll.add (student2);
classRoll.add (student3);
classRoll.add (student4);
classRoll.add (student5);
classRoll.add (student6);

I hope that this is enough info. I can post more of my code if necessary. Thank you for any assistance!


Solution

  • You use the other Collections.sort method: sort(List<T> list, Comparator<? super T> c)

    You use it like this:

    Collections.sort(classRoll, new Comparator<Student>() {
        @Override
        public int compare(Student s1, Student s2) {
            return s1.getName().compareTo(s2.getName());
        }
    });
    

    In Java 8+ it is even easier:

    // Sort by name
    classRoll.sort(Comparator.comparing(Student::getName));
    
    // Sort by GPA
    classRoll.sort(Comparator.comparingDouble(Student::getGpa));
    

    If name is 2 fields (firstName and lastName), you can use thenComparing:

    // Sort by last name, then first name
    classRoll.sort(Comparator.comparing(Student::getLastName)
                             .thenComparing(Student::getFirstName));