1

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!

Erwin Bolwidt
31.3k15 gold badges58 silver badges81 bronze badges
asked Oct 7, 2018 at 20:58
3
  • 1
    "looks like this: ArrayList classRoll = new ArrayList()" I sure hope not, because that would be using raw generics, and you shouldn't do that. It should be: ArrayList<Student> classRoll = new ArrayList<>() Commented Oct 7, 2018 at 21:01
  • docs.oracle.com/javase/7/docs/api/java/util/… Commented Oct 7, 2018 at 21:05
  • 1
    @Andreas absolutely, that's how it is in my code but I'm not sure how the formatting works on this site so it got rid of <Student> after the first ArrayList somehow Commented Oct 7, 2018 at 21:13

2 Answers 2

2

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));
answered Oct 7, 2018 at 21:11

Comments

0

The way to doing this is using the Collections.sort with custom Comparators:

Comparator<Student> nameComparator = new Comparator<Student>() {
 public int compare(Student student1, Student student2) {
 return student1.getName().compareTo(student2.getName());
 }
 };
Comparator<Student> gpaComparator = new Comparator<Student>() {
 public int compare(Student student1, Student student2) {
 return student1.getGPA().compareTo(student2.getGPA());
 }
 };

Then you can use different comparator according to your needs:

Collections.sort(classRoll, nameComparator);

This way you have an optimized sorter when compared to your solution with 2 loops above.

answered Oct 7, 2018 at 21:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.