Java String array sort example shows how to sort string array in Java using Collections class and custom comparator in descending and ascending orders.
How to sort String array in Java?
There are several methods using which you can sort an array of strings.
1) Sort string array using the Arrays class
A string array can be sorted using the sort
method of the Arrays class.
1 |
static void sort(Object[] array) |
This method sorts an array using the natural ordering of the array elements in 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 |
package com.javacodeexamples.basic.arrayexamples; import java.util.Arrays; public class SortStringArrayExample { public static void main(String[] args) { String[] strArray = { "roB","cEntury", "Browser", "ak56", "Raphael", "Aztec", "Playhouse", "Zintec", "zOo", "Accenture", "Yokohama", "xBox", "nIntendo", "PlayStation", "CAke", "boB" }; //sort String array Arrays.sort(strArray); //print array System.out.println(Arrays.toString(strArray)); } } |
Output
1 |
[Accenture, Aztec, Browser, CAke, PlayStation, Playhouse, Raphael, Yokohama, Zintec, ak56, boB, cEntury, nIntendo, roB, xBox, zOo] |
2) Using the Collections class
You can also use the sort
method of Collections class along with the asList
method to sort an array.
1 |
static void sort(List list) |
This method sorts the list in ascending order according to the natural order of its elements.
Note: The sort
method of the Collections class accepts a List as an argument. To sort an array of strings, you first need to convert array to list using the asList
method of the Arrays class.
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 |
package com.javacodeexamples.basic.arrayexamples; import java.util.Collections; public class SortStringArrayExample { public static void main(String[] args) { String[] strArray = { "roB","cEntury", "Browser", "ak56", "Raphael", "Aztec", "Playhouse", "Zintec", "zOo", "Accenture", "Yokohama", "xBox", "nIntendo", "PlayStation", "CAke", "boB" }; //convert list to an array and then sort the list Collections.sort( Arrays.asList(strArray) ); //print array System.out.println(Arrays.toString(strArray)); } } |
Output
1 |
[Accenture, Aztec, Browser, CAke, PlayStation, Playhouse, Raphael, Yokohama, Zintec, ak56, boB, cEntury, nIntendo, roB, xBox, zOo] |
Please note that the asList
method provides an abstract list view on the top of the original array. Hence, sorting a list will also modify the underlying original array.
How to sort array in descending order?
By default, the sort
method of the Arrays and Collections class sorts an array in ascending order. However, you can use the reverseOrder
method of the Collections class to sort array in descending order.
1 |
public static Comparator reverseOrder() |
This method returns a comparator that uses the reverse of the natural ordering of the collection elements (descending order for string elements).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
String[] strArray = { "roB","cEntury", "Browser", "ak56", "Raphael", "Aztec", "Playhouse", "Zintec", "zOo", "Accenture", "Yokohama", "xBox", "nIntendo", "PlayStation", "CAke", "boB" }; //sort String array Arrays.sort(strArray, Collections.reverseOrder()); //print array System.out.println(Arrays.toString(strArray)); |
You can also use the sort
method of the Collections class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
String[] strArray = { "roB","cEntury", "Browser", "ak56", "Raphael", "Aztec", "Playhouse", "Zintec", "zOo", "Accenture", "Yokohama", "xBox", "nIntendo", "PlayStation", "CAke", "boB" }; Collections.sort( Arrays.asList(strArray), Collections.reverseOrder()); //print array System.out.println(Arrays.toString(strArray)); |
Output
1 |
[zOo, xBox, roB, nIntendo, cEntury, boB, ak56, Zintec, Yokohama, Raphael, Playhouse, PlayStation, CAke, Browser, Aztec, Accenture] |
How to sort using custom comparator?
As you may have noticed from the output that when we sort an array using the sort
method, it outputs “PlayStation” before “Playhouse” and “Zintec” before “ak56” string values.
That is because the sort
method sorts string values according to the ASCII values. ASCII value of the capital letter “Z” (90) is less than the ASCII value of the small letter “a” (97) so “Zintec” comes before “ak56” value. What if you want to sort an array of string values regardless of the case of the values? A custom comparator can be used as given below.
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 42 43 44 45 46 47 48 49 50 51 52 |
package com.javacodeexamples.basic.arrayexamples; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; public class SortStringArrayExample { public static void main(String[] args) { String[] strArray = { "roB","cEntury", "Browser", "ak56", "Raphael", "Aztec", "Playhouse", "Zintec", "zOo", "Accenture", "Yokohama", "xBox", "nIntendo", "PlayStation", "CAke", "boB" }; //sort String array using custom comparator Arrays.sort(strArray, customComparator); /* * You can also use sort method of Collections class here like, * Collections.sort( Arrays.asList(strArray), customComparator); */ //print array System.out.println(Arrays.toString(strArray)); } /* * This custom comparator compares the String objects while ignoring the case * of the characters. * */ private static Comparator<String> customComparator = new Comparator<String>(){ public int compare(String string1, String string2){ /* * compareToIgnoreCase method compares the String objects * ignoring the case. */ return string1.compareToIgnoreCase(string2); } }; } |
Output
1 |
[Accenture, ak56, Aztec, boB, Browser, CAke, cEntury, nIntendo, Playhouse, PlayStation, Raphael, roB, xBox, Yokohama, Zintec, zOo] |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.