Ascending Order in Java


Ascending order is an arranged in a series that begins with the least or smallest and ends with the greatest or largest. To define an array named a with n number of integer values. Then, you should use the Arrays. sort() method to sort it. That's how you can sort an array in Java in ascending order using the Arrays.
Example:
{2, 3, 7, 8, 9, 33, 87} are numbers arranged in ascending order. While arranging numbers in ascending order, we write the smallest value first and then we move forward towards the larger values.

Source Code

import java.util.Arrays;
 
public class ascendingOrder {
    public static void main(String args[])
    {
        // Ascending order
        int[] a = new int[]{8, 2, 9, 7, 33, 3, 87};
        System.out.println("Before Sort : "+Arrays.toString(a));
        int temp;
        for(int i=0;i<a.length;i++)
        {
            for(int j=i+1;j<a.length;j++)
            {
                if(a[i]>a[j])
                {
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
            }
        }
        System.out.println("After Sort : "+Arrays.toString(a));
    }
}
 

Output

Before Sort : [8, 2, 9, 7, 33, 3, 87]
After Sort : [2, 3, 7, 8, 9, 33, 87]
To download raw file Click Here

Basic Programs