Java如何按降序对数组值进行排序?
在这里,您将找到一个有关如何按升序或降序对数组值进行排序的示例。
package org.nhooo.example.util;
import java.util.Arrays;
import java.util.Collections;
public class SortArrayWithOrder {
public static void main(String[] args) {
Integer[] points = new Integer[5];
points[0] = 94;
points[1] = 53;
points[2] = 70;
points[3] = 44;
points[4] = 64;
System.out.println("Original : " + Arrays.toString(points));
//对点数组进行排序,默认顺序为升序。
//[44、53、64、70、94]
Arrays.sort(points);
System.out.println("Ascending : " + Arrays.toString(points));
//按降序对点数组进行排序。
//[94,70,64,53,44]
Arrays.sort(points, Collections.reverseOrder());
System.out.println("Descending: " + Arrays.toString(points));
}
}上面的代码片段的结果是:
Original : [94, 53, 70, 44, 64] Ascending : [44, 53, 64, 70, 94] Descending: [94, 70, 64, 53, 44]
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短