I hope this helps you . The title is selfexplanatory. E.g , Short version : code :
Integer[] ary = { 2, 1, 0, 2, 5 };
Arrays.sort(ary, (Integer i1, Integer i2) > Math.abs(i2)  Math.abs(i1));
System.out.println(Arrays.toString(ary));
int[] ary = { 2, 1, 0, 2, 5 };
List<Integer> intList = new ArrayList<Integer>();
for (int index = 0; index < ary.length; index++) {
intList.add(ary[index]);
}
intList.sort((Integer i1, Integer i2) > Math.abs(i2)  Math.abs(i1));
System.out.println(intList);
[5, 2, 2, 1, 0]
Share :

Need help in understanding this specific descending order selectiion sort of integers
By : smstms
Date : March 29 2020, 07:55 AM
this one helps. First of all I would highly suggest to get your code more formatted. In Eclipse it is done by pressing [Ctrl]+[Shift]+[F]. By doing that, your code should look like that (I did some improvements which I will explain then). code :
import java.util.Scanner;
public class Selectionsort_descending {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number");
int n = sc.nextInt();
int a[] = new int[n];
for (int i = 0; i < n ; i++) {
System.out.println("Enter number");
a[i] = sc.nextInt();
}
for (int j = 0; j < n ; j++) {
for (int k = j; k < n; k++) {
if (a[j] < a[k]) {
int m = a[j];
a[j] = a[k];
a[k] = m;
}
}
}
for (int i = 0; i < n ; i++) {
System.out.print(a[i] + " ");
}
sc.close();
}
}

How to sort integer array in ascending and descending order using lambda only in java
By : Martin Gordon
Date : March 29 2020, 07:55 AM
Hope that helps , You could sort the input of type Integer[] as : code :
Integer[] arr2 = new Integer[] {54,432,53,21,43};
Arrays.sort(arr2, Comparator.reverseOrder());
int[] arr2 = new int[]{54, 432, 53, 21, 43};
int[] sortedArray = Arrays.stream(arr2)
.boxed()
.sorted(Comparator.reverseOrder()) // just use 'sorted()' for ascending order
.mapToInt(Integer::intValue)
.toArray();
int[] sortedArray = Arrays.stream(arr2)
.map(i > i).sorted().map(i > i) // just use 'sorted()' for ascending order
// Edit  use map(i > ~i).sorted().map(i > ~i) to be safe from the issue with Integer.MIN_VALUE
.toArray();
int[] arr2 = new int[]{54, 432, 53, 21, 43};
Arrays.sort(arr2);

Partially sort an array in descending order using Java Stream API
By : user3161077
Date : March 29 2020, 07:55 AM
To fix this issue I need to know how to partially sort an array of primitive unique integers in descending order using Stream API. For example, if there is an array like {1,2,3,4,5}, I want to get {5,4,3, 1,2}  3 biggest elements first and then the rest. Is it even possible using streams? I checked the docs  there are two methods skip and limit but they change the stream content and work from the beginning of the array. , Here's an approach using streams. code :
int[] sortPartially(int[] inputArray, int limit) {
Map<Integer, Long> maxValues = IntStream.of(inputArray)
.boxed()
.sorted(Comparator.reverseOrder())
.limit(limit)
.collect(Collectors.groupingBy(x > x, LinkedHashMap::new, Collectors.counting()));
IntStream head = maxValues.entrySet()
.stream()
.flatMapToInt(e > IntStream.iterate(e.getKey(), i > i)
.limit(e.getValue().intValue()));
IntStream tail = IntStream.of(inputArray)
.filter(x > {
Long remainingDuplication = maxValues.computeIfPresent(x, (y, count) > count  1);
return remainingDuplication == null  remainingDuplication < 0;
});
return IntStream.concat(head, tail).toArray();
}
Collection<Integer> sortPartially(int[] inputArray, int sortedPartLength) {
Queue<Integer> pq = new PriorityQueue<>(sortedPartLength);
Deque<Integer> result = IntStream.of(inputArray).boxed().map(x > {
pq.add(x);
return pq.size() > sortedPartLength ? pq.poll() : null;
}).filter(Objects::nonNull).collect(Collectors.toCollection(ArrayDeque::new));
Stream.generate(pq::remove).limit(sortedPartLength).forEach(result::addFirst);
return result;
}

JAVA: Me need sort a series in an array in descending order
By : user3442789
Date : March 29 2020, 07:55 AM
To fix this issue You can sort the array as you already do, and then iterate the array and swap first with last, second with last1, etc. Like in the following code snap: code :
Arrays.sort(twoDimArray[twoDimArray.length  1]);
//Swap
for (int i = 0, j = twoDimArray[twoDimArray.length  1].length  1, tmp; i < j; i++, j) {
tmp = twoDimArray[twoDimArray.length  1][i];
twoDimArray[twoDimArray.length  1][i] = twoDimArray[twoDimArray.length  1][j];
twoDimArray[twoDimArray.length  1][j] = tmp;
}
[7, 3, 6]
int [] a = twoDimArray[twoDimArray.length  1];
Arrays.sort(a);
//Swap
for (int i = 0, j = a.length  1, tmp; i < j; i++, j) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
System.out.println(Arrays.toString(twoDimArray[twoDimArray.length  1]));

How to sort an array of integers in descending order of the first digit?
By : Michele Contesi
Date : March 29 2020, 07:55 AM
wish helps you The ordering you require is a lexicographical descending order. A lexicographical descending order is the order you'll get if you treat the numbers of the input list as Strings and sort them in descending order. If all the integers had the same number of digits, the lexicographical order would be the same as natural ordering of integers.

