To implement the "sort alphabetically" operation, you can use the sort () function of the arrays class under the Java.util package.
The arrays class contains various methods for manipulating arrays, such as sorting and searching.
For example, the sort () function for sorting operations overloads a variety of static functions to suit the needs of different situations.
Below, we apply the last overloaded function to "sort by Chinese first letter":
Copy Code code as follows:
Sort (t[] A, comparator< Super t> C)
Sorts the specified array of objects according to the order in which the comparer is produced.
code example:
Copy Code code as follows:
Package com.app.test;
Import Java.text.Collator;
Import Java.util.Arrays;
Import Java.util.Comparator;
/*
* Java implementation by the Chinese first alphabetical sort of way
*/
public class TestDemo01 {
public static void Main (string[] args) {
The Collator class is used to perform a locale-sensitive String comparison where you choose to use the
Comparator Comparator = collator.getinstance (Java.util.Locale.CHINA);
String[] arrstrings = {"Joe", "Guo Jing", "Yang", "Zhang Mowgli", "Trinket"};
Causes the specified object array to be sorted according to the order produced by the specified comparer.
Arrays.sort (arrstrings, comparator);
for (int i = 0; i < arrstrings.length; i++)
System.out.println (Arrstrings[i]);
}
}
Execution results:
01. Guo Jing
02. Joe
03. Trinket
04. Yang Over
05. Zhang Mowgli
At this point, we have completed the "alphabetical order by Chinese" operation.