Java implementation of the first alphabetical sort of concrete examples _java

Source: Internet
Author: User
Tags collator locale

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.