0

I came up with a small problem while doing my homework.

I have a Person class

public class Person {
    protected String code;
    protected String name;
}

And a subclass called Student

public class Student extends Person implements Comparable<Student>{

    private double grade;

    @Override
    public int compareTo(Student that){
        return Double.compare(grade, that.grade);
    }
}

The teacher wanted us to create a Professor class to manage Student using ArrayList

public class Professor {
    private List<Person> arr;

    public Professor() {
       arr = new ArrayList();
    }
}

After adding about ten students to the ArrayList and we're asked to sort that list. The problem is, as far as I know, I cannot use Collections.sort(arr) since I declared it List< Person >, not List< Student >.

Did I misunderstand anything? Is there any way to use Collections.sort() in this situation or I have to write a method to sort it manually?

And he asked us to implement the Comparable interface in the class Student, also asked us to create List< Person > instead of List< Student >

EDIT: THANK YOU VERY MUCH. APPRECIATE ALL OF THE ANSWERS AND COMMENTS. THE ABOVE PROBLEM HAS BEEN SOLVED BY DOWNCAST List< Person > to List < Student >

4
  • If you plan to sort students in a way particular to students, then the comparator definitely belongs in that class. Commented Jul 2, 2017 at 13:54
  • 1
    Your class Person is most likely an abstract class. Then you can declare Person to implement the Comparable but do the actual code to implementComparable in th the subclasses of Person only Commented Jul 2, 2017 at 13:54
  • @Timothy Truckle I agree with you. With these requirements, it seems the most logical way. Commented Jul 2, 2017 at 14:11
  • 1
    Just think about it this way: If your List<Person> had a Teacher in it, how would you sort that with respect to the Students in the list? Commented Jul 2, 2017 at 14:22

2 Answers 2

3

The teacher wanted us to create a Professor class to manage Student using ArrayList

So replace

private List<Person> arr;

by :

private List<Student> students;

In this way, you could invoke Collections.sort(students) as student refers to a List of Comparable elements (Student implementing the Comparable interface).


Edit after the comment :

The requirement is not very logical : you have to sort something (List<Person>) that is not sortable (as only Student implements Comparable).

In theses conditions, you have three ways :

  • the simplest and the cleanest way (if you may use it).
    Make Person an abstract class that implementsComparable but keep it abstract in.
    In this way you would not need to use any cast. You could directly invoke Collections.sort() with a List<Person>

  • force the downcast from List<Person> to List<Student> and invoke then Collections.sort().

  • perform yourself the sort but you should still do some casts if you want to use the compareTo() method of the Student class as Person is not a Comparable instance.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. The thing is he forced us to use List < Person >. It really annoys the whole class
You are welcome. The requirement is really not logical. I updated with 3 propositions.
Very appreciate it @davidxxx . It works well with the downcast option.
You are very welcome :) FIY : If an answer suits to your requirement, you should accept the answer rather than editing your question to add this information.
0

It is not allowing as person because the collection sort required to implement implements Comparable<Person> as the API is.

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

One way is as mentioned below post I am providing you another approach for sorting list using Comparator

class SortbyGrade implements Comparator<Student>
{
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student a, Student b)
    {
         return Double.compare(a.grade, b.grade);
    }
}

//collection sort

Collections.sort(arr, new SortbyGrade());

Comments

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.