java - 如何根据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();
解决方案
通过使用Collections.sort()
升序
Collections.sort(studentList,Comparator.comparingDouble(Student::getGradePointsAverage));
降序
Collections.sort(list,Comparator.comparingDouble(Student::getGradePointsAverage).reversed());
推荐阅读
- css - 带有 CSS 的圆形滑块
- ruby-on-rails - Rails 5.1 - 按关联值排序的显示列表
- python - 如何使用终端输入作为 Python 参数?
- javascript - 只有三元运算符在这里工作
- mysql - ON UPDATE CURRENT_TIMESTAMP 在使用相同信息更新时不起作用
- google-app-engine - 在推送队列(GAE)中使用命名空间 api 的多租户
- php - 停止 PHP 自动生成值
- ios - 如何快速随机化一组精灵?
- python - TensorFlow GPU 的安装
- identityserver4 - IdentityServer4 跨不同身份验证流维护会话 ID