日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/3/26 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Comparator::compare设定排序的升序 降序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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;//默認交換,表示降序 }

demo代碼

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 readabilitySystem.out.println("初始化順序");// Iterating over entries to print themfor (int i = 0; i < ar.size(); i++)System.out.println(ar.get(i));// Sorting student entries by roll numberCollections.sort(ar, new SortbyrollAsc());// Display message on console for better readabilitySystem.out.println("\n自定義升序排序");// Again iterating over entries to print themfor (int i = 0; i < ar.size(); i++)System.out.println(ar.get(i));// Sorting student entries by roll numberCollections.sort(ar, new SortbyrollDsc());// Display message on console for better readabilitySystem.out.println("\n自定義降序排序");// Again iterating over entries to print themfor (int i = 0; i < ar.size(); i++)System.out.println(ar.get(i));Collections.sort(ar, new DefaultDsc());// Display message on console for better readabilitySystem.out.println("\n默認強制降序排序");// Again iterating over entries to print themfor (int i = 0; i < ar.size(); i++)System.out.println(ar.get(i));Collections.sort(ar, new DefaultAsc());// Display message on console for better readabilitySystem.out.println("\n默認強制升序排序");// Again iterating over entries to print themfor (int i = 0; i < ar.size(); i++)System.out.println(ar.get(i));} }

總結

以上是生活随笔為你收集整理的Comparator::compare设定排序的升序 降序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。