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.
- Types of Statements in Java (Best Example)
- what are access specifiers in java
- types of access specifiers in java
- What is Polymorphism in java
- What is Interface in Java
- Bubble sort in java with explanation
- BufferedReader Class in Java
- What is Abstract Class in Java
- What is Lambda Expression in Java 8
- What is Exception Handling in Java
- Types of Exceptions in Java
- What is the Final Keyword in Java
- What is Wrapper Class in Java
- Break Keywords in Java with Example
Top Array Programming interview questions
- What is an array and what are its advantages?
- How do you declare an array in your programming language of choice?
- What is the difference between an array and a linked list?
- How do you traverse an array in your programming language of choice?
- What is the time complexity of accessing an element in an array?
- How do you find the length of an array in your programming language of choice?
- How do you insert an element into an array?
- How do you delete an element from an array?
- What is the time complexity of inserting or deleting an element from an array?
- What is a dynamic array and how is it different from a static array?
- How do you resize a dynamic array?
- What is a two-dimensional array and how do you declare it?
- How do you traverse a two-dimensional array?
- How do you sort an array in your programming language of choice?
- What is the time complexity of the sorting algorithm you use to sort an array?
- How do you search for an element in an array?
- What is a sparse array and how is it different from a regular array?
- How do you implement a stack using an array?
- How do you implement a queue using an array?
- What are the limitations of using an array and when would you use a different data structure instead?
- Java Program to remove duplicate elements in an Array (Best Program)
- Write a java Program to find the duplicate Characters in a String
- Array in Java With Example Program
- WAP to remove duplicate elements in an array
Join Telegram | Join Now |
Home Page | Full Stack With Java |