How to Sort Array in Java in descending Order [Best Program 2]

How to Sort Array in Java in descending order: To sort an array in Java in descending order, you can use the Arrays.sort() method along with a custom Comparator that specifies the descending order.

Here’s an example code snippet:

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

public class SortArrayDescending {
    public static void main(String[] args) {
        int[] arr = {5, 2, 8, 1, 6};
        
        // Sort array in descending order using Comparator
        Integer[] integerArr = Arrays.stream(arr).boxed().toArray(Integer[]::new);
        Arrays.sort(integerArr, Comparator.reverseOrder());
        
        // Print sorted array
        System.out.println(Arrays.toString(integerArr));
    }
}

In this example, we first convert the primitive int array to an Integer object array using the Arrays.stream() and boxed() methods. Then, we pass this array and a Comparator that specifies the reverse order (Comparator.reverseOrder()) to the Arrays.sort() method. Finally, we print the sorted array using Arrays.toString().

How to Sort Array in java in descending order using for loop

To sort an array in descending order using a for loop in Java, you can use the following code:

int[] arr = {5, 2, 8, 1, 9};

for (int i = 0; i < arr.length; i++) {
    for (int j = i+1; j < arr.length; j++) {
        if (arr[i] < arr[j]) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
}

// print the sorted array
for (int i = 0; i < arr.length; i++) {
    System.out.print(arr[i] + " ");
}

This code sorts the array in descending order using a nested for loop. The outer for loop iterates over each element of the array, and the inner for loop compares the current element with every other element in the array. If the current element is less than the compared element, their positions are swapped.

After sorting, the code prints the sorted array.

Top Array Programming interview questions

  1. What is an array and what are its advantages?
  2. How do you declare an array in your programming language of choice?
  3. What is the difference between an array and a linked list?
  4. How do you traverse an array in your programming language of choice?
  5. What is the time complexity of accessing an element in an array?
  6. How do you find the length of an array in your programming language of choice?
  7. How do you insert an element into an array?
  8. How do you delete an element from an array?
  9. What is the time complexity of inserting or deleting an element from an array?
  10. What is a dynamic array and how is it different from a static array?
  11. How do you resize a dynamic array?
  12. What is a two-dimensional array and how do you declare it?
  13. How do you traverse a two-dimensional array?
  14. How do you sort an array in your programming language of choice?
  15. What is the time complexity of the sorting algorithm you use to sort an array?
  16. How do you search for an element in an array?
  17. What is a sparse array and how is it different from a regular array?
  18. How do you implement a stack using an array?
  19. How do you implement a queue using an array?
  20. What are the limitations of using an array and when would you use a different data structure instead?
Join TelegramJoin Now
Home PageFull Stack With Java
Sort Array in Java in descending order

Leave a Comment