Write a program to Sort Numeric Array In Ascending Order


This program sorts an array of integers in ascending order using the Arrays.sort() method

  • First, an array of integers arr is initialized with some values. Then, the original array is printed using the Arrays.toString() method.
  • Next, the Arrays.sort() method is used to sort the array in ascending order. This method modifies the original array and sorts it in place.
  • Finally, the sorted array is printed using the Arrays.toString() method. The output of this program will be the original array followed by the sorted array.

Source Code

import java.util.Arrays; 
class Ascending 
{ 
	public static void main(String[] args) 
    { 
		int[] arr = {23,5,67,20,3,30,79,3,70,2};
		System.out.println("Original Array : "+ Arrays.toString(arr));  
		Arrays.sort(arr);  
		System.out.print("Sorted Array : "+ Arrays.toString(arr)); 
    } 
}

Output

Original Array : [23, 5, 67, 20, 3, 30, 79, 3, 70, 2]
Sorted Array : [2, 3, 3, 5, 20, 23, 30, 67, 70, 79]

Example Programs