Comparator::compare设定排序的升序 降序
生活随笔
收集整理的這篇文章主要介紹了
Comparator::compare设定排序的升序 降序
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
java.util.Comparator中 compare(T o1, T o2) 函數(shù),其實(shí)現(xiàn)決定升序降序。舉例如下:對(duì)某個(gè)對(duì)象的var類例進(jìn)行排序
int compare(T o1, T o2) {return o1.var - o2.var; }理解升序、降序的三步走:
1 明確 兩個(gè)變量o1 o2表示序列中前 后的兩個(gè)變量;位置關(guān)系:o1 在前;o2居后;
2 明確返回值表達(dá)是否交換位置,正數(shù):交換兩者位置; 負(fù)數(shù): 不交換;
3 判斷是降序還是升序。
升序
int compare(T o1, T o2) {return o1.var - o2.var; }?分析如下:
1 明確o1在前, o2在后;
2 若o1.var-o2.var為正,則交換位置;為負(fù),不交換位置;
3 若上述為正條件成立,則表明o1.var為大數(shù),大數(shù)置后,故為升序;
降序
int compare(T o1, T o2) {return o2.var - o1.var; }?分析如下:
1 明確o1在前, o2在后;
2 若o2.var-o1.var為正,則交換位置;為負(fù),不交換位置;
3 若上述為正條件成立,則表明o2.var為大數(shù),大數(shù)置前,故為降序;
默認(rèn)升序/降序
int compare(T o1, T o2) {return -1;//默認(rèn)不交換,表示升序return 1;//默認(rèn)交換,表示降序 }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 //默認(rèn)強(qiáng)制降序 class DefaultDsc implements Comparator<Student> {public int compare(Student a, Student b) {return 1;} }//default dsc //默認(rèn)強(qiáng)制升序 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默認(rèn)強(qiáng)制降序排序");// 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默認(rèn)強(qiáng)制升序排序");// Again iterating over entries to print themfor (int i = 0; i < ar.size(); i++)System.out.println(ar.get(i));} }總結(jié)
以上是生活随笔為你收集整理的Comparator::compare设定排序的升序 降序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 最新微信支付,andro
- 下一篇: 随机生成不重复的字符和数字