Google News
logo
Java Program to Sort an Array in Ascending Order
In the following example of Java program to sort an array in ascending order using the built-in Arrays.sort() method :
Program :
import java.util.Arrays;

public class SortArrayAscending {
    public static void main(String[] args) {
        int[] arr = {5, 7, 18, 3, 1, 12, 36};
        Arrays.sort(arr);
        System.out.println("Array sorted in ascending order: " + Arrays.toString(arr));
    }
}
Output :
Array sorted in ascending order: [1, 3, 5, 7, 12, 18, 36]
In this program, we first declare an integer array arr and initialize it with the values {5, 7, 18, 3, 1, 12, 36};.

We then use the Arrays.sort() method to sort the array in ascending order. Finally, we print the sorted array to the console using System.out.println() and Arrays.toString().