Sorting Arrays in Java

Hello Friends,

I would like to welcome you to my blog.

This is the tutorial for all my java followers. We all must be familiar with arrays and sorting arrays.

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

Hope this will help you.
Thanks and regards,
Nikhil Naoghare.

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

4 Comments

  1. found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.