2

I have an ArrayList composed of Student objects. These objects contain first name, last name, lab grade, project grade, exam grade, and total grade. I am trying to write a function that sorts the Student objects in the ArrayList based on their total grade.

Here's my Student class:

public class Student {

// fields
private String firstname;
private String lastname;
private int labgrade;
private int projectgrade;
private int examgrade;
private int totalgrade;

// constructor
public Student(String firstname, String lastname, int labgrade,
        int projectgrade, int examgrade) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.labgrade = labgrade;
    this.examgrade = examgrade;
    this.totalgrade = labgrade + projectgrade + examgrade;
}

// method
public String toString() {
    String s = firstname + " " + lastname + " has a total grade of " 
            + totalgrade;
    return s;
}
public int compareTo(Student s) {
    return (totalgrade = s.totalgrade);
}

And here's what I tried to do to sort:

private ArrayList<Student> arraylist = new ArrayList<Student>();
public void SortStudent() {
    Collections.sort(arraylist);
}

But that doesn't work because it says it can only work on List not ArrayList. Any help to fix my SortStudent method?

2
  • An ArrayListis a List. Commented May 2, 2016 at 18:17
  • Your compareTo() is wrong. totalgrade = s.totalgrade is an assignment, not a comparison. To compare, use return Integer.compare(this.totalgrade, s.totalgrade). Commented May 2, 2016 at 18:27

3 Answers 3

4

ArrayList is a List, the problem is that Student does not implement Comparable, and you didn't define the compareTo method, which you need to do to use the sort method from Collections.
You could do something similar to this:

public class Student implements Comparable<Student> {
    //... the rest of the class
    public int compareTo(Student s) {
        return Integer.compare(this.grade, s.grade);
    }
}

Another option would be to use lambda expressions with the method Collections.sort(List<T>, Comparator<? super T> c), where you don't need to implement Comparable:

public void sortStudent() {
    Collections.sort(arraylist, (s1, s2) -> Integer.compare(s1.grade, s2.grade));
}
Sign up to request clarification or add additional context in comments.

1 Comment

implements Comparable -> implements Comparable<Student>
1

Collections.sort only works for types that implement Comparable:

public static <T extends Comparable<? super T>> void sort(List<T> list) {...}

Implementing Comparable<...> with Student is a bad choice in this case, since the sorting criteria are likely to change. e.g. sometimes you want to sort by first name, and sometimes by last name.

List<...> has a sort method that takes a Comparator, you could use that:

private ArrayList<Student> arraylist = new ArrayList<Student>();
...
public void SortStudent() {
    arraylist.sort((x, y) -> Integer.compare(x.totalgrade, y.totalgrade));
}

Comments

-1
  class Student implements Comparable{  
          int rollno;  
          String name;  
          int grade;  
    Student(int rollno,String name,int grade){  
       this.rollno=rollno;  
       this.name=name;  
       this.grade=grade;
    }  

    public int compareTo(Object obj){  
      Student st=(Student)obj;  
      if(grade==st.grade)  
      return 0;  
      else if(grade>st.grade)  
      return 1;  
       else  
    return -1;  
    }  

2 Comments

Okay I'm impressed you can implement a compareTo, but how does this help? You've offered no explanation.
with comparable, you specify how your object are to be compered

Your Answer

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.