How to Sort Int Array in Java


In this example we will show how to sort an int array using sort method of Arrays class of java.util package.

Source Code

int array
package com.beginner.examples;

import java.util.Arrays;

public class SortIntArray {

	public static void main(String[] args) {
		
		//create int array
		int[] intArray = new int[]{103,125,25,78,95};
		
		System.out.print("Before sorting , IntArray contains : ");
		for(int index=0; index < intArray.length ; index++) {
			System.out.print("  "  + intArray[index]);
		}
		
		/*
		 * To sort java int array use sort method of Arrays.
		 */
		Arrays.sort(intArray);
		
		System.out.println();
		System.out.print("After sorting , IntArray contains : ");
		for(int index=0; index < intArray.length ; index++) {
			System.out.print("  "  + intArray[index]);
		}
		

	}

}

Output:

Before sorting , IntArray contains :   103  125  25  78  95
After sorting , IntArray contains :   25  78  95  103  125

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments