# Java 数组降序排序？

#### Java Array Sort descending?

27 个回复

sort(T[] a, Comparator<? super T> c)

Arrays.sort(a, Collections.reverseOrder());

Arrays.sort()不能直接用于按降序对原始数组进行排序。 如果您尝试通过传递Collections.reverseOrder()定义的反向 Comparator 来调用Arrays.sort()方法，则会抛出错误

Collections.sort(list, Collections.reverseOrder());

Arrays.sort(array, Collections.reverseOrder());

Arrays.sort(data, Collections.reverseOrder());

Collections.reverseOrder()使用逆自然顺序返回一个Comparator 您可以使用Collections.reverseOrder(myComparator)获得您自己的比较器的反转版本。

1. 将数组乘以 -1
2. 种类
3. 再次乘以-1

array = -Arrays.sort(-array)

Collections.sort(list, Collections.reverseOrder());

Collections.sort(list, Collections.reverseOrder(new Comparator()));

// an array of ints
int[] arr = {1, 2, 3, 4, 5, 6};

// an array of reverse sorted ints
int[] arrDesc = Arrays.stream(arr).boxed()
.sorted(Collections.reverseOrder())
.mapToInt(Integer::intValue)
.toArray();

System.out.println(Arrays.toString(arrDesc)); // outputs [6, 5, 4, 3, 2, 1]

Arrays.sort(list, comparator.reversed());

Collections.sort(myArray);

Collections.reverse(myArray);

ArrayUtils.reverse(array);

public int compareTo(freq arg0)
{
int ret=0;
if(this.magnitude>arg0.magnitude)
ret= 1;
else if (this.magnitude==arg0.magnitude)
ret= 0;
else if (this.magnitude<arg0.magnitude)
ret= -1;
return ret;
}

public int compareTo(freq arg0)
{
int ret=0;
if(this.magnitude>arg0.magnitude)
ret= -1;
else if (this.magnitude==arg0.magnitude)
ret= 0;
else if (this.magnitude<arg0.magnitude)
ret= 1;
return ret;
}

array.sort(function(a, b) {return b - a;}); //descending

array.sort(function(a, b) {return a - b;}); //ascending

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] nums = { 5, 4, 1, 2, 9, 7, 3, 8, 6, 0 };
Arrays.sort(nums);

// reverse the array, just like dumping the array!
// swap(1st, 1st-last) <= 1st: 0, 1st-last: nums.length - 1
// swap(2nd, 2nd-last) <= 2nd: i++,  2nd-last: j--
// swap(3rd, 3rd-last) <= 3rd: i++,  3rd-last: j--
//
for (int i = 0, j = nums.length - 1, tmp; i < j; i++, j--) {
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}

// dump the array (for Java 4/5/6/7/8/9)
for (int i = 0; i < nums.length; i++) {
System.out.println("nums[" + i + "] = " + nums[i]);
}
}
}

nums[0] = 9
nums[1] = 8
nums[2] = 7
nums[3] = 6
nums[4] = 5
nums[5] = 4
nums[6] = 3
nums[7] = 2
nums[8] = 1
nums[9] = 0

Arrays.sort(array, (o1, o2) -> o2 - o1);

Arrays.sort(array, (o1, o2) -> o2.getValue() - o1.getValue());

// our initial int[] array containing primitives
int[] arrOfPrimitives = new int[]{1,2,3,4,5,6};

// we have to convert it into array of Objects, using java's boxing
Integer[] arrOfObjects = new Integer[arrOfPrimitives.length];
for (int i = 0; i < arrOfPrimitives.length; i++)
arrOfObjects[i] = new Integer(arrOfPrimitives[i]);

// now when we have an array of Objects we can use that nice built-in method
Arrays.sort(arrOfObjects, Collections.reverseOrder());

arrOfObjects现在是{6,5,4,3,2,1} 如果您有一个非整数数组 - 使用相应的对象而不是Integer

Integer[] arr = {7, 10, 4, 3, 20, 15};
Arrays.sort(arr, Collections.reverseOrder());

int[] arr = {7, 10, 4, 3, 20, 15};
int[] reverseArr = IntStream.rangeClosed(1, arr.length).map(i -> arr[arr.length-i]).toArray();

int[][] array= {
{1, 5},
{13, 1},
{12, 100},
{12, 85}
};
Arrays.sort(array, (a, b) -> Integer.compare(a[1], b[1])); // for ascending order
Arrays.sort(array, (b, a) -> Integer.compare(a[1], b[1])); // for descending order

12, 100
12, 85
1, 5
13, 1

package doublearraysort;

import java.util.Arrays;
import java.util.Collections;

public class Gpa {

public static void main(String[] args) {
// initializing unsorted double array
Double[] dArr = new Double[] {
new Double(3.2),
new Double(1.2),
new Double(4.7),
new Double(3.3),
new Double(4.6),
};
// print all the elements available in list
for (double number : dArr) {
System.out.println("GPA = " + number);
}

// sorting the array
Arrays.sort(dArr, Collections.reverseOrder());

// print all the elements available in list again
System.out.println("The sorted GPA Scores are:");
for (double number : dArr) {
System.out.println("GPA = " + number);
}
}
}

GPA = 3.2
GPA = 1.2
GPA = 4.7
GPA = 3.3
GPA = 4.6
The sorted GPA Scores are:
GPA = 4.7
GPA = 4.6
GPA = 3.3
GPA = 3.2
GPA = 1.2
public double[] sortArrayAlgorithm(double[] array) { //sort in descending order
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] >= array[j]) {
double x = array[i];
array[i] = array[j];
array[j] = x;
}
}
}
return array;
}

List<String> items = new ArrayList<>();

items.stream()
.sorted()
.forEach(item -> System.out.println(item));

items.stream()
.sorted(Comparator.reverseOrder())
.forEach(item -> System.out.println(item));

private static int[] descendingArray(int[] array) {
Arrays.sort(array);
int[] descArray = new int[array.length];
for(int i=0; i<array.length; i++) {
descArray[i] = array[(array.length-1)-i];
}
return descArray;
}

import java.util.Arrays;
import java.util.Comparator;
...

Integer[] aInt = {6,2,3,4,1,5,7,8,9,10};
Arrays.sort(aInt, Comparator.reverseOrder()  );

import java.util.Arrays;
import java.util.Collections;
class sortdesc{
public static void main(String[] args){
// int Array
Integer[] intArray=new Integer[]{
new Integer(15),
new Integer(9),
new Integer(16),
new Integer(2),
new Integer(30)};

// Sorting int Array in descending order
Arrays.sort(intArray,Collections.reverseOrder());

// Displaying elements of int Array
System.out.println("Int Array Elements in reverse order:");
for(int i=0;i<intArray.length;i++)
System.out.println(intArray[i]);

// String Array
String[] stringArray=new String[]{"FF","PP","AA","OO","DD"};

// Sorting String Array in descending order
Arrays.sort(stringArray,Collections.reverseOrder());

// Displaying elements of String Array
System.out.println("String Array Elements in reverse order:");
for(int i=0;i<stringArray.length;i++)
System.out.println(stringArray[i]);}}

javac sortdec.java

java sortdesc

Int Array Elements in reverse order:
30
16
15
9
2
String Array Elements in reverse order:
PP
OO
FF
DD
AA

//replace this line:
String[] stringArray=new String[]{"FF","PP","AA","OO","DD"};

//with this:
String[] stringArray=new String[]{"10FF","20AA","50AA"};

50AA
20AA
10FF

public static int[] sortArray (int[] array) {
int [] sortedArray = new int[array.length];
for (int i = 0; i < sortedArray.length; i++) {
sortedArray[i] = array[i];
}

boolean flag = true;
int temp;
while (flag) {
flag = false;
for (int i = 0; i < sortedArray.length - 1; i++) {
if(sortedArray[i] < sortedArray[i+1]) {
temp = sortedArray[i];
sortedArray[i] = sortedArray[i+1];
sortedArray[i+1] = temp;
flag = true;
}
}
}

return sortedArray;

}

public static int[] sortArrayDesc(int[] intArray){
Arrays.sort(intArray);                      //sort intArray in Asc order
int[] sortedArray = new int[intArray.length];   //this array will hold the sorted values

int indexSortedArray = 0;
for(int i=intArray.length-1 ; i >= 0 ; i--){    //insert to sortedArray in reverse order
sortedArray[indexSortedArray ++] = intArray [i];
}
return sortedArray;
}

int[] elements = new int [] {10,999,999,-58,548,145,255,889,1,1,4,5555,0,-1,-52};
//int[] elements = null;

if(elements != null && elements.length >1)
{
int max = 0, index = 0;
for(int i =0;i<elements.length;i++)//find out what is Max
{
if(elements[i] > max)
{
max = elements[i];
index = i;
}
}
elements[index] = elements[0];//Swap the places
elements[0] = max;
for(int i =0;i < elements.length;i++)//loop over element
{
for(int j = i+1;j < elements.length;j++)//loop to compare the elements
{
if(elements[j] > elements[i])
{
max = elements[j];
elements[j] = elements[i];
elements[i] = max;
}
}
}

}//i ended up using three loops and 2 extra variables
System.out.println(Arrays.toString(elements));//if null it will print null

int[] intArr = new int[] {9,4,1,7};
Arrays.sort(nums);
Collections.reverse(Arrays.asList(nums));

[1, 4, 7, 9]

Arrays.sort(a, Comparator.reverseOrder());

FWIW 列表

Lists.reverse(a);

Collections.reverse(a);
1 将Java中的数组按降序排序

2 Java：按降序对未排序的数组进行排序

2010-04-18 18:17:40 5 4803   java
3 Java：未排序的数组到降序的数组

2017-05-15 13:05:19 3 59   java
4 在java中按降序对数组进行排序

5 Java中对象数组的降序选择排序

6 JAVA：按降序对数组进行排序

2017-05-04 11:55:16 4 441   java
7 已排序？ 检查数组是否在 Java 中按升序或降序排序

8 选择按降序排序-JAVA

9 数组排序降序

10 按降序排序MATLAB数组

2013-09-16 03:47:31 1 15334   indexing