首页 > 解决方案 > 如何根据GPA对数组列表进行升序排序?arraylist 包含一个对象数组。(爪哇)

问题描述

我正在尝试根据 GPA 按升序对这个数组列表进行排序。学生类封装了 3 条信息:学号、姓氏、平均绩点。

所以我需要使用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();

标签: javaarrayssortingarraylist

解决方案


通过使用Collections.sort()升序

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

降序

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

推荐阅读