package com.itheima.d1_set;
/*
判断比较器compare升序降序
*/
import sun.plugin.javascript.navig.Array;
import java.util.Arrays;
import java.util.Comparator;
public class SetDemo4 {
public static void main(String[] args) {
Integer array[] ={10,3,7,2,20,24};
Arrays.sort(array,new Comparator<Integer>(){
@Override
public int compare(Integer t1, Integer t2) {
return t1-t2; //升序
}
});
for (Integer arrays : array){
System.out.print(arrays+" ");
}
}
}
结果:2 3 7 10 20 24
package com.itheima.d1_set;
import sun.plugin.javascript.navig.Array;
import java.util.Arrays;
import java.util.Comparator;
public class SetDemo4 {
public static void main(String[] args) {
Integer array[] ={10,3,7,2,20,24};
Arrays.sort(array, new Comparator<Integer>() {
@Override
public int compare(Integer t1, Integer t2) {
return t2-t1; //降序
}
});
for (Integer arrays : array){
System.out.print(arrays+" ");
}
}
}
结果:24 20 10 7 3 2
发现:public int compare(Integer t1, Integer t2) {
return t1-t2; //升序
}
public int compare(Integer t1, Integer t2) {
return t2-t1; //降序
}
即:当返回的参数名称与形参名顺序相同时,则为升序。反之为降序。