Grails Cookbook - A collection of tutorials and examples

Sort String Array in Java

String Array is a very useful data structure when we need to hold multiple String values. It is very simple to understand for programmers with background in some other language. It is also easy to use for many programming scenarios. But one common thing we usually need when working with Arrays, is that we sometimes need the data to be ordered or sorted. Below are some examples on how to Sort String Array in Java.

Bubble Sort

If we wish to study some well known algorithms, we may employ using our own implementation. Below is a sample code on how to sort String Array in Java using bubble sort. Note that this sorting algorithm is somehow slow. It's Big O complexity is worse than other algorithms. But one advantage of it is that it is easy to understand for people who wish to study how sorting works. Students may start to study this before looking at faster but more complicated way of sorting arrays.
/**
 * A simple example that uses Bubble Sort on a Sring Array in Java
 */
public class SampleBubbleSort {
    public static void bubbleSort(String[] stringArray) {
        int n = stringArray.length;
        String temp;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < (n - i); j++) {
                if (stringArray[j - 1].compareTo( stringArray[j] ) > 0) {
                    temp = stringArray[j - 1];
                    stringArray[j - 1] = stringArray[j];
                    stringArray[j] = temp;
                }

} } } private static void printArray( String[] stringArray ) { for (String s:stringArray) { System.out.print( s + " " ); } } public static void main( String[] args ) { String[] mySampleStringArray = { "Z", "D", "A", "W", "B", "Y", "C", "X" }; bubbleSort( mySampleStringArray ); printArray(mySampleStringArray); } }

As expected, sorting the string in the example above produces this output:
A B C D W X Y Z 

Quick Sort

If we want a faster but more complicated way, below is an example of using Quick Sort to arrange Java String Array from lowest to highest value.
/**
 * A simple example that uses QuickSort on a Sring Array in Java
 */
public class SampleQuickSort {
    public static int partition(String[] stringArray, int idx1, int idx2) {
        String pivotValue = stringArray[idx1];
        while (idx1 < idx2) {
           String value1;
           String value2;
           while ((value1 = stringArray[idx1]).compareTo( pivotValue ) < 0) {
               idx1 = idx1 + 1;
           }
           while ((value2 = stringArray[idx2]).compareTo( pivotValue ) > 0) {
               idx2 = idx2 - 1;
           }
           stringArray[idx1] = value2;
           stringArray[idx2] = value1;
        }
        return idx1;
    }
    public static void QuicksortString(String[] stringArray, int idx1, int idx2) {
        if (idx1 >= idx2) {
            // we are done
            return;
        }
        int pivotIndex = partition(stringArray, idx1, idx2);
        QuicksortString(stringArray, idx1, pivotIndex);
        QuicksortString(stringArray, pivotIndex+1, idx2);
     }
    public static void QuicksortString(String[] stringArray) {
        QuicksortString(stringArray, 0, stringArray.length - 1);
    }
    private static void printArray( String[] stringArray )
    {
        for (String s:stringArray) {
            System.out.print( s + " " );
        }
    }
    public static void main( String[] args )
    {
        String[] mySampleStringArray = { "Z", "D", "A", "W", "B", "Y", "C", "X" };
        QuicksortString( mySampleStringArray );
        printArray(mySampleStringArray);
    }
|
Quick Sort is developed by Tony Hoare in 1959 and published in 1961. If you wish to know more about this algorithm, you may visit this page and this page.

Arrays.sort

For practical purposes, we really don't need to do our own implementation. Java's SDK ships out with an implementation of sorting using Arrays.sort(). As of the time of this writing, the documentation says that the algorithm used is Timsort. Below is a simple example on how to use Arrays.sort() on a String Aarray.
/**
 * A simple example that uses Arrays.sort() on a Sring Array in Java
 */
public class SampleArraysSort {
    private static void printArray( String[] stringArray )
    {
        for (String s:stringArray) {
            System.out.print( s + " " );
        }
    }
    public static void main( String[] args ) {
        String[] mySampleStringArray = { "Z", "D", "A", "W", "B", "Y", "C", "X" };
        Arrays.sort(mySampleStringArray );
        printArray(mySampleStringArray);
    }
}