Java: String Array sorting with example

In this article, we will discuss how to sort String[] array with example

Arrays class has various sort methods for sorting different primitive data-types and Objects.

 

Sorting String[] array:

To sort String[] array, we have 2 variant of sort methods from Arrays class

Method Signature:

public static void sort(Object[] a);

public static void sort(Object[] a, int fromIndex, int toIndex);

 

String[] array methods for sorting:

Sort method

Description

sort(obj[]); sorts complete String[] array
sort(obj[], sIndex, eIndex); sorts partial String[] array, as per limits specified in the start-index & end-index

 

Let us move forward to discuss both methods for sorting String[] array

Method 1: complete sorting of String[] array

  • Here, complete array will be sorted

Method Signature:

public static void sort(obj[] a);

 

SortingCompleteStringArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingCompleteStringArray {

	public static void main(String[] args) {

		// sample String[] array
		String[] strArray = {"Sachin", "Dravid", "Ganguly", 
				"Laxman", "Sehwag", "Kohli", "Dhoni"};

		// before sorting
		System.out.println("Before sorting : \n");
		for(String str : strArray) {
			System.out.println(str);
		}

		// sorting full String[] array
		Arrays.sort(strArray);

		// after sorting
		System.out.println("\n\nAfter sorting : \n");
		for(String str : strArray) {
			System.out.println(str);
		}
	}
}

Output:

Before sorting : 

Sachin
Dravid
Ganguly
Laxman
Sehwag
Kohli
Dhoni


After sorting : 

Dhoni
Dravid
Ganguly
Kohli
Laxman
Sachin
Sehwag

 

Method 2: partial sorting of String[] array

  • This is the another variant to sort array where we can specify start & end limits within String[] array

Method Signature:

public static void sort(obj[] a, int fromIndex, int toIndex);

 

SortingPartialStringArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingPartialStringArray {

	public static void main(String[] args) {

		// sample String[] array
		String[] strArray = {"Sachin", "Dravid", "Ganguly", 
				"Laxman", "Sehwag", "Kohli", "Dhoni"};

		// before sorting
		System.out.println("Before sorting : \n");
		for(String str : strArray) {
			System.out.println(str);
		}

		// sorting partial String[] array
		Arrays.sort(strArray, 1, 6);

		// after sorting
		System.out.println("\n\nAfter sorting : \n");
		for(String str : strArray) {
			System.out.println(str);
		}
	}
}

Output:

Before sorting : 

Sachin
Dravid
Ganguly
Laxman
Sehwag
Kohli
Dhoni


After sorting : 

Sachin
Dravid
Ganguly
Kohli
Laxman
Sehwag
Dhoni

 

Explanation:

  • Here, there are 7 String elements in String[] array
  • But, we have sorted String[] array starting from index-1 till index-5 leaving 1st & last element
  • Therefore, 1st and last element remains as it is after sorting and only middle elements are sorted

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

 

Happy Coding !!
Happy Learning !!