当前位置:   article > 正文

Comparator::compare设定排序的升序 降序_java compare方法怎么排序的

java 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<Student> {
  18. public int compare(Student a, Student b) {
  19. return a.rollno - b.rollno;
  20. }
  21. }
  22. //自定义降序
  23. class SortbyrollDsc implements Comparator<Student> {
  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<Student> {
  31. public int compare(Student a, Student b) {
  32. return 1;
  33. }
  34. }
  35. //default dsc
  36. //默认强制升序
  37. class DefaultAsc implements Comparator<Student> {
  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<Student> ar = new ArrayList<Student>();
  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. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/355128
推荐阅读
相关标签
  

闽ICP备14008679号