In this article, I recorded how MySQL is implemented to sort names alphabetically, and here's how this sort is implemented in Java. The code is as follows:
1 PackageCom.review.chapter6;2 3 ImportJava.text.Collator;4 Importjava.util.Collections;5 ImportJava.util.Comparator;6 Importjava.util.Enumeration;7 ImportJava.util.Locale;8 ImportJava.util.Vector;9 Ten Importorg.junit.Test; One A /** - * This example demonstrates sorting by phonetic order in the name - * @authorZhouxy the * - */ - Public classGetsortofchinesename { - @Test + Public voidGetsort () { -Vector<string> v =NewVector<string>(); +String[] names = {"Zhang San", "Zhang Si", "Wang San", "Harry", "Li er", "Li Liu"}; A for(inti = 0; I < names.length;i++){ at v.addelement (Names[i]); - } - -Comparator cmp = collator.getinstance (Locale.china);//get the collator of the desired locale -Collections.sort (v, CMP);//Sorts v according to the order produced by the specified comparer - inEnumeration<string> e =v.elements (); - while(E.hasmoreelements ()) { to System.out.println (E.nextelement ()); + } - } the}
The key part of the code is the two lines of my comment, first producing a comparer (comparator), and then calling the sort method in the collections class. Locale.china indicates that the required environment is Chinese.
Phonetic sorting of names in Java