Java program to sort the elements in descending order
In this post, you will learn how to sort the elements in descending order using Java programming language such that elements will be arranged from largest to smallest. There are different ways to sort the array in descending order, here we have mentioned two of them.
Java sort array in descending order using the forloop
In this program, we need to sort the given array in descending order such that elements will be arranged from largest to smallest. In the given program, we have used two loops to sort the array in descending order. The outer loop will select an element, and the inner loop allows us to compare the selected element with the rest of the elements. In the forloop body, we have compared the adjacent elements and swapped if they are not in order. For this, we have used a temporary variable for the swapping of elements.
public class SortArrayElements {
public static void main(String[] args) {
//Initialize array
int [] array = new int [] {43, 13, 63, 28, 35, 72};
int temp = 0;
//Sort the array in descending order
for (int i = 0; i < array.length; i++) {
for (int j = i+1; j < array.length; j++) {
if(array[i] < array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println();
//Displaying elements of array after sorting
System.out.println("Elements of array in descending order: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
Output of the above code:
Elements of array in descending order:
72 63 43 35 28 13
Java sort array in descending order using the sort() method
A collection represents a group of objects, known as its elements. Collections is a utility class that resides in the java.util package, it consists entirely of static methods which are used to operate on collections. In the given program, we have defined an array of type integer. After that, we have used the sort() method of the Arrays class and passed the array to be sort and reverseOrder() method of the collection to get the array in descending order.
import java.util.*;
public class SortArrayDesc {
public static void main(String[] args)
{
//Initializing an array of integer type
Integer array[] = { 43, 23, 64, 13, 53};
// Sorting the array in descending order
Arrays.sort(array, Collections.reverseOrder());
// Printing the elements
System.out.println(Arrays.toString(array));
}
}
Output of the above code:
[64, 53, 43, 23, 13]
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java