题解 | #输入整型数组和排序标识,对其元素按照升序或降序进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
http://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=sc.nextInt();
Integer[] arr=new Integer[count];
for(int i=0;i<count;i++){
arr[i]=sc.nextInt();
}
int flag=sc.nextInt();
if(flag==0){
Arrays.sort(arr,new Comparator<Integer>(){
public int compare(Integer o1,Integer o2){
return o1-o2;
}
});
}
if(flag==1){
//Arrays.sort(arr,new Comparator<Integer>(){
//public int compare(Integer o1,Integer o2){
//return o2-o1;
//}
//});
Arrays.sort(arr,(o1,o2)->o2-o1);
}
for(int m:arr){
System.out.print(m+" ");
}
}
}
上海得物信息集团有限公司公司福利 1208人发布