标签:float space bool font png 编译过程 was mamicode ever
升序
<p>//以下nums为数组</p>
<p> 例如nums数组有5个元素,那么对其升序排序应该为sort(nums,nums+5);</p>1. JAVA的降序是Arrays.sort(nums,Collections.reverseOrder()),但由于此方法不支持基本类型(int,float,double,char等),所以在使用的时候要将int型转换为Integer,flaot为Float。
这里的Integer型转换为int型可以用Integer.valueOf(num)。
例如.
public static void main(String args[]){
      Integer[] nums = {2,3,1,4,5};
      Arrays.sort(nums,Collections.reverseOrder());
      int[] tmp = new int[nums.length];
      for(int i  = 0;i < nums.length;++i){
            tmp[i] = Integer.valueOf(nums[i]);
      }
      for(int i : tmp){
            System.out.print(i + " ");
      }
}
结果图:

2. C++则是用sort(nums,nums + 5,compare);
#include <algorithm>
#include<iostream>
using namespace std; 
bool compare(a,b){
      return a > b;//降序排列,如果改成a < b则为升序
}
int main(){
      int nums[] = {2,3,1,4,5};
      sort(nums,nums + 5,compare);
      for(int i = 0;i < 20;++i)
            cout<<nums[i]<<endl;
      return 0;
}
结果图:

标签:float space bool font png 编译过程 was mamicode ever
原文地址:https://www.cnblogs.com/Charlo/p/13953870.html