0

How to sort array of objects(i.e array of objects of class student) by marks attribute but without using comparator?

    class Student
{
    private int rollNo;
    private double marks;
    public Student(int rollNo, String name, double marks) {
        this.rollNo = rollNo;
        this.marks = marks;
    }
}
Rajat
  • 9
  • 1

2 Answers2

1

If you want to use the standard Java sort implementation, then modify Student so that it implements Comparable<Student> .... appropriately. Then search the Javadocs to find the method for sorting a list. (Hint: look for "sort" in the javadoc index ...)

If you can't do that, then you may need to implement a sort algorithm from scratch.

That should be enough to get you started on your homework :-)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You should create get() and set() methods for the Student class.

Then, you can loop through your array of Student objects and use get() to compare.

Set and Get Methods in java?

Community
  • 1
  • 1
heaerk
  • 1
  • 1