In this program, we will take a look at how you can sort an Array in Java in Ascending or Descending order
First let's create an array with random numbers.
int[] randomNumberArray = {76,34,23,89,77,22,45,1,38};
Sorting in Ascending Order
Now to sort this array of numbers in ascending order we can make use of the Arrays.sort method.
Arrays.sort(randomNumberArray);
Finally let's iterate through the array and print the sorted array values
for (int no: randomNumberArray) {
System.out.print(no +"\t");
}
Sorting in Descending Order
Note that there is no revere function in the Arrays utility class to reverse an Array and make it in descending order. So we have to either use Collections class or write our own logic.
public static void main(String[] args) {
int[] randomNumberArray = {76,34,23,89,77,22,45,1,38};
Arrays.sort(randomNumberArray);
// Reverse the array
for (int i = 0; i < randomNumberArray.length / 2; i++) {
int temp = randomNumberArray[i];
randomNumberArray[i] = randomNumberArray[randomNumberArray.length - i - 1];
randomNumberArray[randomNumberArray.length - i - 1] = temp;
}
for (int no: randomNumberArray) {
System.out.print(no +"\t");
}
}
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- Java 8 - Convert List to Map Examples - Java
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted. - Android
- Fetch only content-type using cURL Command - cURL
- How to remove password from pdf file - HowTos
- How to delete SharePoint Online List Item using REST API - SharePoint
- How to add to PATH in macOS Big Sur - MacOS
- Java Program: Find max value in List using Java 8 Stream API - Java
- SharePoint list excel import error - Title is a required filed and can't be empty - SharePoint