Java Tip: How to Sort Arrays in Java using java.util.Arrays class

Here is a small but very useful tip that every Java programmer should be aware of. Have you ever tried sorting arrays in Java? Well, java.util.Arrays class has built in method to make your job easy. You can use following method to sort any array in Java.

import java.util.Arrays;
...
...
Arrays.sort (int [])
Arrays.sort (String [])
Arrays.sort (float [])
Arrays.sort (double [])
Arrays.sort (long [])
Arrays.sort (Object [])
...

Let us check an example were we will sort an array of String in ascending as well as descending order. Here is a string array we defined in Java.

String [] stringArray = {"ab", "aB", "c", "0", "2", "1Ad", "a10"};
		
System.out.println("****** Unsorted String Array *******");
for (String str : stringArray) {
	System.out.println(str);
}

Output:

****** unsorted string *******
ab
aB
c
0
2
1Ad
a10

In above code we simple define an array of String and printed its value. Now lets sort this array in ascending order using Arrays.sort() method.

Sort in Ascending Order

//Sort array in ascending order
Arrays.sort(stringArray);

System.out.println("****** Sorted String Array *******");
for (String str : stringArray) {
	System.out.println(str);
}

Output:

****** Sorted String Array *******
0
1Ad
2
a10
aB
ab
c

Note that we just sorted an array of String in ascending order using sort method. Wasn’t it easy..

Sort in Descending Order

Now lets try to sort the array in reverse order. For this we will use a different signature of sort method.

Arrays.sort (Object [], Comparator)

Following is the code to sort array in reverse order.

//Sort array in reverse order
Arrays.sort(stringArray, Collections.reverseOrder());

System.out.println("****** Reverse Sorted String Array *******");
for (String str : stringArray) {
	System.out.println(str);
}

Output:

****** Reverse Sorted String Array *******
c
ab
aB
a10
2
1Ad
0

Selective Sorting

Using Arrays.sort() method it is possible to sort an array selectively. i.e. if you want a subpart of array to be sorted, that is possible using following method.

Arrays.sort (Object [], int startIndex, int endIndex)

In following code we are sorting the array starting from index 3 till the end.

//Sorting array starting from index 3 till 6
Arrays.sort(stringArray, 3, 6);

System.out.println("****** Selective Sort String Array *******");
for (String str : stringArray) {
	System.out.println(str);
}

Output:

****** Selective Sort String Array *******
ab
aB
c
0
1Ad
2
a10

Happy sorting.. :)



12 Comments

  • Mike Ault 12 July, 2010, 18:17

    I process quite a few AWR and Statspack reports where SQLNet roundtrips per transaction are extremely high. The solution is to use array passing more efficiently in the calling program. Many times this program is java or javascript. Can you elucidate the method to turn on array passing between Oracle, java and javascript for result sets?

  • krishna 22 July, 2010, 7:43

    how jvm is linked with internet connection.

  • Yoo 14 September, 2010, 13:57

    I have a question to you

    When I typed “contacts.jsp”,

    My eclipse told me that “The markup in the document preceding the root element must be well-formed.” in the first line

    Is it wrong or not?
    Due to this problem, I cannot see succesful result.

  • Yoo 15 September, 2010, 7:26

    Hello, I have a question to you.

    When I made a “contacts.jsp”, It made an error.

    I am using Eclipse Helios, Tomcat 7.0, JDK 1.6,

    Um… What’s a problem? In my opinion, JDK couldn’t look for this direction about tld….

  • Farhan 22 January, 2012, 10:14

    A nice tutorial, but the Arrays.sort() methods uses a tuned quicksort with a time complexity of ) O(n*lgn).. Have a look at other sorting algos Counting Sort,Heap Sort. You shld also tell your users about the stability of the algorithms.

  • Anadi Pattanaik 31 May, 2012, 16:17

    Hi All,
    I want a alphanumeric sorting in which small letter comes first and capital letter will come in last.
    Can anyone help me by doing a java code.
    input’s like :
    3A
    3A(1)
    3A(2)
    3(A)
    3(A)(1)
    3(A)(2)
    4A
    4A(1)
    4A(2)
    4(A)(1)
    4(A)(2)
    3a
    3(a)(1)
    3(a)(2)
    4a
    4(a)(1)
    4(a)(2)

    out put like:
    3(A)
    3(a)(1)
    3(A)(1)
    3(a)(2)
    3(A)(2)
    3a
    3A
    3A(1)
    3A(2)
    4(a)(1)
    4(A)(1)
    4(a)(2)
    4(A)(2)
    4a
    4A
    4A(1)
    4A(2)

  • Vinh 22 March, 2013, 23:15

    THANK YOU!

  • Piyush 31 March, 2013, 15:26

    nice turial bro…
    thanks

  • Yashwanth 15 September, 2013, 11:56

    in my program
    if am using Arrays.sort()
    getting compile time error .classs expected
    can u plz resolve the problem…

    • Vinod Kumar Singh 27 September, 2013, 15:51

      U can use sort() method of Arrays Class for sorting the Array Values
      public class SortString {
      public static void main(String[] args) {
      String[] str=new String[5];
      str[0]=”vinod”;
      str[1]=”Anmol”;
      str[2]=”Raju”;
      str[3]=”Dheeraj”;
      str[4]=”Binu”;
      for(int i=0;i<5;i++){
      Arrays.sort(str, 0,5);
      System.out.println(str[i]);
      }
      }
      }
      output is :
      Anmol
      Binu
      Dheeraj
      Raju
      vinod

  • Pawan Soni 12 October, 2013, 16:01

    Nice Explanation !!!!!!!!!!!!!!!!!

  • creatives infotech 5 March, 2014, 18:02

    Your post is really great. Its just saved my day to code.

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]