赞
踩
java.util.Comparator中 compare(T o1, T o2) 函数,其实现决定升序降序。举例如下:对某个对象的var类例进行排序
- int compare(T o1, T o2) {
- return o1.var - o2.var;
- }
理解升序、降序的三步走:
1 明确 两个变量o1 o2表示序列中前 后的两个变量;位置关系:o1 在前;o2居后;
2 明确返回值表达是否交换位置,正数:交换两者位置; 负数: 不交换;
3 判断是降序还是升序。
- int compare(T o1, T o2) {
- return o1.var - o2.var;
- }
分析如下:
1 明确o1在前, o2在后;
2 若o1.var-o2.var为正,则交换位置;为负,不交换位置;
3 若上述为正条件成立,则表明o1.var为大数,大数置后,故为升序;
- int compare(T o1, T o2) {
- return o2.var - o1.var;
- }
分析如下:
1 明确o1在前, o2在后;
2 若o2.var-o1.var为正,则交换位置;为负,不交换位置;
3 若上述为正条件成立,则表明o2.var为大数,大数置前,故为降序;
- int compare(T o1, T o2) {
- return -1;//默认不交换,表示升序
- return 1;//默认交换,表示降序
- }
- package bob.util;
- import java.lang.*;
- import java.util.*;
-
- class Student {
- int rollno;
- String name, address;
-
- public Student(int rollno, String name, String address) {
- this.rollno = rollno;
- this.name = name;
- this.address = address;
- }
-
- public String toString() {
- return this.rollno + " " + this.name + " " + this.address;
- }
- }
-
- //自定义升序
- class SortbyrollAsc implements Comparator<Student> {
- public int compare(Student a, Student b) {
- return a.rollno - b.rollno;
- }
- }
-
- //自定义降序
- class SortbyrollDsc implements Comparator<Student> {
- public int compare(Student a, Student b) {
- return b.rollno - a.rollno;
- }
- }
-
- //default asc
- //默认强制降序
- class DefaultDsc implements Comparator<Student> {
- public int compare(Student a, Student b) {
- return 1;
- }
- }
-
- //default dsc
- //默认强制升序
- class DefaultAsc implements Comparator<Student> {
- public int compare(Student a, Student b) {
- return -1;
- }
- }
-
- // Main class
- public class SortDemo {
- public static void main(String[] args) {
- ArrayList<Student> ar = new ArrayList<Student>();
- ar.add(new Student(141, "Mayank", "london"));
- ar.add(new Student(131, "Anshul", "nyc"));
- ar.add(new Student(161, "Solanki", "jaipur"));
- ar.add(new Student(151, "Aggarwal", "Hongkong"));
- // Display message on console for better readability
- System.out.println("初始化顺序");
- // Iterating over entries to print them
- for (int i = 0; i < ar.size(); i++)
- System.out.println(ar.get(i));
-
- // Sorting student entries by roll number
- Collections.sort(ar, new SortbyrollAsc());
- // Display message on console for better readability
- System.out.println("\n自定义升序排序");
- // Again iterating over entries to print them
- for (int i = 0; i < ar.size(); i++)
- System.out.println(ar.get(i));
-
- // Sorting student entries by roll number
- Collections.sort(ar, new SortbyrollDsc());
- // Display message on console for better readability
- System.out.println("\n自定义降序排序");
- // Again iterating over entries to print them
- for (int i = 0; i < ar.size(); i++)
- System.out.println(ar.get(i));
-
- Collections.sort(ar, new DefaultDsc());
- // Display message on console for better readability
- System.out.println("\n默认强制降序排序");
- // Again iterating over entries to print them
- for (int i = 0; i < ar.size(); i++)
- System.out.println(ar.get(i));
-
- Collections.sort(ar, new DefaultAsc());
- // Display message on console for better readability
- System.out.println("\n默认强制升序排序");
- // Again iterating over entries to print them
- for (int i = 0; i < ar.size(); i++)
- System.out.println(ar.get(i));
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。