Skip to main content

Sorting array element in java

To sort Array element you can use Arrays.sort(..) method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
package com.spy.javaforlearn;
 
import java.util.Arrays;
 
public class SortingArray 
{
 
	public static void main(String[] args) 
	{
 
         int[] number=new int[5];
 
		//getting size of Array
 
	     System.out.println("Size of Array: "+number.length); 
 
	    //initializing Array
 
	     number[0]=10;
	     number[1]=30;
	     number[2]=15;
	     number[3]=23;
	     number[4]=17;
 
	     //accessing array element
 
	     System.out.println("accessing array element");
 
	     for(int i=0;i<number.length;i++)
	      {
	    	 System.out.println(number[i]);	 
	      }
 
 
		//Sorting Element
	     Arrays.sort(number);
 
	     System.out.println("After Sorting Array Element");
 
	     for(int i=0;i<number.length;i++)
	      {
	    	 System.out.println(number[i]);	 
	      }
 
	}
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
 
accessing array element
10
30
15
23
17
After Sorting Array Element
10
15
17
23
30