Java api makes the sorting vary easy with respect to the Strings. In the previous tutorials we have discussed about the bean sorting using comparator interface and comparable interface. when comes to the string array we don’t want to implement any such kind of interfaces to sort a string array. By using  the Arras class we can sort a string array easily.

In this tutorials we can sort a string array using Java api and also we can sort the array logically.

String sorting in Java

[sourcecode language=”java”]

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author chandrashekhar
 */
public class StringSorting {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter Array Size : ");
        int n = s.nextInt();
        String names[] = new String[n];
        System.out.println("Enter any "+n+" names");
        for (int i = 0; i < n; i++) {
            names[i]=s.next();
        }
        System.out.println("\n Before Sorting");
        for (int i = 0; i < n; i++) {
            System.out.print(names[i]+" ,");
        }
        Arrays.sort(names);
        System.out.println(" \n After Sorting");
        for (int i = 0; i < n; i++) {
            System.out.print(names[i]+" ,");
        }
         
    }
}

[/sourcecode]

Output:

Enter Array Size :
5
Enter any 5 names
rahul
sachin
akhil
john
admin

Before Sorting
rahul ,sachin ,akhil ,john ,admin ,
After Sorting
admin ,akhil ,john ,rahul ,sachin ,

In the above example, for sorting an string array I used sort(String[]) method. sort(String[]) is a static method which takes the array of strings as a parameter and sort the strings in natural order.

String sorting Example

[sourcecode language=”java”]

import java.util.Scanner;

/**
 *
 * @author chandrashekhar
 */
public class StringSorting {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter Array Size : ");
        int n = s.nextInt();
        String names[] = new String[n];
        System.out.println("Enter any "+n+" names");
        for (int i = 0; i < n; i++) {
            names[i]=s.next();
        }
        System.out.println("\n Before Sorting");
        for (int i = 0; i < n; i++) {
            System.out.print(names[i]+" ,");
        }
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if(names[j].compareToIgnoreCase(names[j+1])>0){
                    String temp=names[j];
                    names[j]=names[j+1];
                    names[j+1]=temp;
                }
            }
        }
        System.out.println(" \n After Sorting");
        for (int i = 0; i < n; i++) {
            System.out.print(names[i]+" ,");
        }
         
    }
}

[/sourcecode]

Output

Enter Array Size :

5
Enter any 5 names
john
robert
anika
scott
swiss

Before Sorting
john ,robert ,anika ,scott ,swiss ,
After Sorting
anika ,john ,robert ,scott ,swiss ,

You may also Like

Bean Sorting using comparator interface

Bean Sorting using comparable interface