• Comparator::compare设定排序的升序 降序


    java.util.Comparator中 compare(T o1, T o2) 函数,其实现决定升序降序。举例如下:对某个对象的var类例进行排序

    1. int compare(T o1, T o2) {
    2. return o1.var - o2.var;
    3. }

    理解升序、降序的三步走:

    1 明确 两个变量o1 o2表示序列中前 后的两个变量;位置关系:o1 在前;o2居后;

    2 明确返回值表达是否交换位置,正数:交换两者位置; 负数: 不交换;

    3 判断是降序还是升序。

    升序

    1. int compare(T o1, T o2) {
    2. return o1.var - o2.var;
    3. }

     分析如下:

    1 明确o1在前, o2在后;

    2 若o1.var-o2.var为正,则交换位置;为负,不交换位置;

    3 若上述为正条件成立,则表明o1.var为大数,大数置后,故为升序;

    降序

    1. int compare(T o1, T o2) {
    2. return o2.var - o1.var;
    3. }

     分析如下:

    1 明确o1在前, o2在后;

    2 若o2.var-o1.var为正,则交换位置;为负,不交换位置;

    3 若上述为正条件成立,则表明o2.var为大数,大数置前,故为降序;

    默认升序/降序

    1. int compare(T o1, T o2) {
    2. return -1;//默认不交换,表示升序
    3. return 1;//默认交换,表示降序
    4. }

    demo代码

    1. package bob.util;
    2. import java.lang.*;
    3. import java.util.*;
    4. class Student {
    5. int rollno;
    6. String name, address;
    7. public Student(int rollno, String name, String address) {
    8. this.rollno = rollno;
    9. this.name = name;
    10. this.address = address;
    11. }
    12. public String toString() {
    13. return this.rollno + " " + this.name + " " + this.address;
    14. }
    15. }
    16. //自定义升序
    17. class SortbyrollAsc implements Comparator {
    18. public int compare(Student a, Student b) {
    19. return a.rollno - b.rollno;
    20. }
    21. }
    22. //自定义降序
    23. class SortbyrollDsc implements Comparator {
    24. public int compare(Student a, Student b) {
    25. return b.rollno - a.rollno;
    26. }
    27. }
    28. //default asc
    29. //默认强制降序
    30. class DefaultDsc implements Comparator {
    31. public int compare(Student a, Student b) {
    32. return 1;
    33. }
    34. }
    35. //default dsc
    36. //默认强制升序
    37. class DefaultAsc implements Comparator {
    38. public int compare(Student a, Student b) {
    39. return -1;
    40. }
    41. }
    42. // Main class
    43. public class SortDemo {
    44. public static void main(String[] args) {
    45. ArrayList ar = new ArrayList();
    46. ar.add(new Student(141, "Mayank", "london"));
    47. ar.add(new Student(131, "Anshul", "nyc"));
    48. ar.add(new Student(161, "Solanki", "jaipur"));
    49. ar.add(new Student(151, "Aggarwal", "Hongkong"));
    50. // Display message on console for better readability
    51. System.out.println("初始化顺序");
    52. // Iterating over entries to print them
    53. for (int i = 0; i < ar.size(); i++)
    54. System.out.println(ar.get(i));
    55. // Sorting student entries by roll number
    56. Collections.sort(ar, new SortbyrollAsc());
    57. // Display message on console for better readability
    58. System.out.println("\n自定义升序排序");
    59. // Again iterating over entries to print them
    60. for (int i = 0; i < ar.size(); i++)
    61. System.out.println(ar.get(i));
    62. // Sorting student entries by roll number
    63. Collections.sort(ar, new SortbyrollDsc());
    64. // Display message on console for better readability
    65. System.out.println("\n自定义降序排序");
    66. // Again iterating over entries to print them
    67. for (int i = 0; i < ar.size(); i++)
    68. System.out.println(ar.get(i));
    69. Collections.sort(ar, new DefaultDsc());
    70. // Display message on console for better readability
    71. System.out.println("\n默认强制降序排序");
    72. // Again iterating over entries to print them
    73. for (int i = 0; i < ar.size(); i++)
    74. System.out.println(ar.get(i));
    75. Collections.sort(ar, new DefaultAsc());
    76. // Display message on console for better readability
    77. System.out.println("\n默认强制升序排序");
    78. // Again iterating over entries to print them
    79. for (int i = 0; i < ar.size(); i++)
    80. System.out.println(ar.get(i));
    81. }
    82. }
  • 相关阅读:
    JavaScript中this的绑定规则
    什么是生产流程管理系统?
    Hbase 面试题(六)
    使用Lychee搭建个人图片存储系统并进行远程访问设置实现公网访问本地私人图床
    5.2 创建个人中心页面-前端部分
    开源数字基础设施 项目 -- Speckle
    linux安装Android打包环境
    Sql优化(详解一条龙服务)
    C语言小项目 -- 通讯录(静态版+动态版+文件版)
    高优先线程
  • 原文地址:https://blog.csdn.net/cheetahzhang/article/details/127747457