Here you will find an example on how to sort the values of an array in ascending or descending order.
package org.kodejava.example.util;
import java.util.Arrays;
import java.util.Collections;
public class SortArrayWithOrder {
public static void main(String[] args) {
Integer[] points = new Integer[5];
points[0] = 94;
points[1] = 53;
points[2] = 70;
points[3] = 44;
points[4] = 64;
System.out.println("Original : " + Arrays.toString(points));
// Sort the points array, the default order is in ascending order.
// [44, 53, 64, 70, 94]
Arrays.sort(points);
System.out.println("Ascending : " + Arrays.toString(points));
// Sort the points array in descending order.
// [94, 70, 64, 53, 44]
Arrays.sort(points, Collections.reverseOrder());
System.out.println("Descending: " + Arrays.toString(points));
}
}
The result of the code snippet above are:
Original : [94, 53, 70, 44, 64]
Ascending : [44, 53, 64, 70, 94]
Descending: [94, 70, 64, 53, 44]
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019