Java sort string array containing numbers example shows how to sort string array containing numbers using a comparator in ascending and descending order.
How to sort string array containing numbers?
Java Arrays class provides the sort
method which can be used to sort a string array.
1 |
static void sort(Object[] array) |
This method sorts an array according to the natural ordering of the elements in the ascending order.
Let’s give it a try to see what happens when we try to sort a string array containing numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.javacodeexamples.basic.sorting; import java.util.Arrays; import java.util.Comparator; public class SortStringArrayNumbersExample { public static void main(String[] args) { String[] strNumberArray = { "1", "11", "121", "112", "13", "101", "115" }; //sort array using sort method of Arrays class Arrays.sort(strNumberArray); //print the String array System.out.println( Arrays.toString(strNumberArray) ); } } |
Output
1 |
[1, 101, 11, 112, 115, 121, 13] |
As you may have noticed from the output, the numbers are not sorted by their numeric values. This happens because sort method sorts string values based on the ASCII value of its characters. The ASCII value of zero is less than the ASCII value of 1, so “101” comes before the “11”.
In order to sort string values according to their numeric values, we need to write a custom Comparator as given below. The Comparator interface has the compare
method which needs to be implemented by the implementing class.
1 |
int compare(T object1, T object2) |
The compare
method should return 0 for the equal objects, a positive value if the object1 is greater than object2, and a negative value if the object1 is less than the object2 for sorting the objects in an ascending order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.javacodeexamples.basic.sorting; import java.util.Arrays; import java.util.Comparator; public class SortStringArrayNumbersExample { public static void main(String[] args) { String[] strNumberArray = { "1", "11", "121", "112", "13", "101", "115" }; //sort array using custom comparator Arrays.sort(strNumberArray, new StringNumberComparator()); System.out.println(Arrays.toString(strNumberArray)); } } class StringNumberComparator implements Comparator<String>{ public int compare(String strNumber1, String strNumber2) { //convert String to int first int number1 = Integer.parseInt( strNumber1 ); int number2 = Integer.parseInt( strNumber2 ); //compare numbers if( number1 > number2 ){ return 1; }else if( number1 < number2 ){ return -1; }else{ return 0; } } } |
Output
1 |
[1, 11, 13, 101, 112, 115, 121] |
How to sort in descending order?
In the above example, we sorted an array of strings in the ascending order according to the numeric values of its elements. In order to sort an array in descending order, we need to change the custom comparator a bit.
The compare
method should return 0 for the same objects, a negative value if object1 is greater than object2 and positive value if object1 is less than object2 if array needs to be sorted in descending order as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public int compare(String strNumber1, String strNumber2) { int number1 = Integer.parseInt( strNumber1 ); int number2 = Integer.parseInt( strNumber2 ); if( number1 > number2 ){ return -1; }else if( number1 < number2 ){ return 1; }else{ return 0; } } |
Output
1 |
[121, 115, 112, 101, 13, 11, 1] |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.