0

I'm trying to sort this arraylist based on GPA in ascending order. Student class encapsulates 3 pieces of information: Student Number, Last Name, Grade Point Average.

So I need to sort them using gradePointAverage.

//student array
studentArray[0] = student;
studentArray[1] = new Student(9093891, "Brown", 2.55);
studentArray[2] = new Student(9301879, "Carson", 1.11);
studentArray[3] = new Student(3910880, "Deardon", 4.01);
studentArray[4] = new Student(8891783, "Ellis", 2.66);
studentArray[5] = new Student(3899132, "Fisher", 0.55);

//filling my studentlist
studentList.add(student);
studentList.add(studentArray[0]);
studentList.add(studentArray[1]);
studentList.add(studentArray[2]);
studentList.add(studentArray[3]);
studentList.add(studentArray[4]);

ArrayList<String> sortedArrayListDescending = studentList.sortDescending();
Maz
  • 17
  • 6
  • I know it says studentList.sortDescending() but even that doesn't work. That was messing around. I get an "error: cannot find symbol" – Maz Mar 30 '19 at 01:05
  • "*I get an "error: cannot find symbol"*" that is because there is no `sortDescending` nor `sortAscending` methods in List, there is `sort` method which takes `Comparator` which represents mechanism used to check among two compared objects first is equal, bigger or smaller than second one. – Pshemo Mar 30 '19 at 01:10

1 Answers1

1

By using Collections.sort() ascending order

Collections.sort(studentList,Comparator.comparingDouble(Student::getGradePointsAverage));

Descending order

Collections.sort(list,Comparator.comparingDouble(Student::getGradePointsAverage).reversed());
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98