Phonetic sorting of names in Java

Source: Internet
Author: User
Tags collator

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

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.