Program : Sort an Array in Java

Following is the code to sort an int Array in Java in decreasing order. That means the highest will appear first and the smallest will be in last.
package info.FiveBalloons.Examples;

import java.util.Scanner;

public class ArraySorting {
	
        public static Scanner scanner= new Scanner(System.in);
	
        public static void main(String args[]){
                int[] myIntArray = getIntegers(5);
                sortArray(myIntArray);
                printArray(myIntArray);
        }
	
        public static int[] getIntegers(int number){
                System.out.println("Please Enter "+number+" integer values");
                int intArray[] = new int[number];
                for(int i=0; i<number; i++){
                    intArray[i] = scanner.nextInt();
                }
                return intArray;
        }
	
        public static void printArray(int[] intArray){
               for(int i=0; i<intArray.length; i++){
                     System.out.println(intArray[i]);
               }
        }
	
        public static void sortArray(int[] intArray){
                boolean flag = true;
                while(flag){
                        flag= false;
                        for(int i=0; i< intArray.length-1; i++){
                               if(intArray[i] <= intArray[i+1]){
                                        int temp = intArray[i];
                                        intArray[i] = intArray[i+1];
                                        intArray[i+1] = temp;
                                        flag = true;
                                }
                        }
                 }
        }

}

Code Explanation In the above code we have used java.util.Scanner Class to accept input from the user. getInegers(int number) method accepts the input and returns a int array. sortArray(int[] intArray) accepts the intArray and sorts it in descending order. Consider the for loop. Inside for loop we compare the one Array element value with the next value. If the first is greater less than the second then we swap the values. Since we want to sort the array in descending order. We keep doing this until we have our flag as false which denotes what all the values are now in descending order. The above code is traditional way of sorting an Array. There is another fast and quick way to sort Array via Arrays utility class that is provided in the Java library. Using sort method one can sort array of any data type including String. Here is how you can do it.
public class ArraySorting {
	
        public static void main(String args[]){
            int[] myIntArray = {43,32,53,23,12};
            String[] myStringArray = {"Bob","Alice","Tim","John","Denise"};
		
            //Sort Array 
            Arrays.sort(myIntArray);
            Arrays.sort(myStringArray);
		
            System.out.println(Arrays.toString(myIntArray));
            System.out.println(Arrays.toString(myStringArray));
		
        }
}
We have used two utility methods in this code, sort method is used to sort the Array Elements and toString method is used to convert Array into string readable representation.

Site Footer