List sorting is no stranger to programmers, and the collections.sort () method can be anything you want. Today’s scheme is based on this method, mixing letters, Chinese characters, numbers and other special characters.

Everyone in the work will certainly encounter the situation of the list sort, the following introduction to a mixture of letters, Chinese characters, numbers and other kinds of special characters sorting solution, I hope to help you.

Now suppose a scenario, for example, need to read the system address book, and then group and sort according to the name and display, the rules are as follows: the name at the beginning of the letter is directly sorted according to the alphabet; Names at the beginning of Chinese characters are sorted according to the position of the Chinese phonetic alphabet; Numbers and other characters are displayed at the bottom of the list. The expected effect is shown as follows:

Since we implement it using the collections.sort () method, the most important thing is to implement a Comparator that meets our requirements. According to the above rules, we can proceed with the following steps:

Convert the name

Here are the rules:

  • Characters are letters that are converted directly to uppercase letters;
  • Characters for Chinese, converted to the corresponding pinyin, and then pinyin converted to capital;
  • The character is a number or other characters and will not be processed

The logic is simple: each character of the name is matched with a re, and then processed according to the rules above. I use TinyPinyin to transfer Chinese characters to pinyin, you can also choose other tools to achieve (Pinyin4J, JPinyin, etc.).

The converted name is processed as needed

Determine whether to process the converted string based on the first character of the name: if the first character is a letter or Chinese character, no processing is done; if the first character is a number or other character, add any character whose Unicode value is greater than the letter to the string obtained in the first step. This is useful to ensure that names beginning with numbers and other characters are always at the end of the list when comparing (because the collections.sort () method is based on comparison sorting of characters in the order of their Unicode list of characters).

The getLetter() method gets the first character corresponding to letters, numbers, and other characters and returns “#” directly.

If you don’t need to deal with sorting numbers and other characters, you can omit this step

The above two steps can be combined into one method:

Custom Comparator methods

Here is no more to say ~

The sorting

Finally, call the collections.sort () method

Code implementation is actually very simple, I believe we will, mainly the scheme. Attached code address, I hope to help you, thank you.