Sorts the Chinese characters of an attribute in a Java object set by pinyin.

Source: Internet
Author: User

During development, sometimes we need to sort the attributes of objects in a set in pinyin order to help us quickly find Chinese characters. I use the Comparator interface, define sorting rules in this interface, get the Chinese character encoding of the comparison object, convert it into a String, and use the String classThe compareTo () method compares the two objects. After talking about this, why don't I post code...

1. Create an object entity class ProvinceVO to be sorted.

/*
* Copyright 2013 The JA-SIG Collaborative. All rights reserved.
* Distributed with this file and available online
* Http://www.etong.com/
*/
Package snippet;

Import java. io. Serializable;

/**
* Object entity classes to be sorted
*
* @ Author Gan weibin Exp
* @ Version $ FileName: ProvinceVO. java $ Date: 2:11:06-0400 2013
* @ Since 1.0
*
*/
Public class ProvinceVO implements Serializable {
Private static final long serialVersionUID =-6808503832740453598L;
/**
* ID
*/
Private String id;
/**
* Name
*/
Private String name;

Public String getId (){
Return id;
}

Public void setId (String id ){
This. id = id;
}

Public String getName (){
Return name;
}

Public void setName (String name ){
This. name = name;
}
}

2. Create a comparator class PinYinComparator.

/*
* Copyright 2013 The JA-SIG Collaborative. All rights reserved.
* Distributed with this file and available online
* Http://www.etong.com/
*/
Package snippet;

Import java. util. Comparator;

/**
*
* @ Author Gan weibin Exp
* @ Version $ FileName: descClass. java $ Date: 2:26:42-0400 2013
* @ Since 1.0
*
*/
Public class PinYinComparator implements Comparator <ProvinceVO> {
/**
* Implement a method in the interface to implement comparison conditions <br>
* Get the Chinese character encoding of the comparison object and convert it into a string
*/
@ Override
Public int compare (ProvinceVO arg1, ProvinceVO arg2 ){
Try {
String str1 = new String (arg2.getName (). getBytes ("GB2312"), "ISO-8859-1 ");
String str2 = new String (arg1.getName (). getBytes ("GB2312"), "ISO-8859-1 ");
Return str2.compareTo (str1 );
} Catch (Exception e ){
Return 0;
}
}
}

3. This is the Test class.

/*
* Copyright 2013 The JA-SIG Collaborative. All rights reserved.
* Distributed with this file and available online
* Http://www.etong.com/
*/
Package snippet;

Import java. util. ArrayList;
Import java. util. Collections;
Import java. util. List;

/**
*
* @ Author Gan weibin Exp
* @ Version $ FileName: Snippet. java $ Date: 1:49:24-0400 2013
* @ Since 1.0
*
*/

Public class Test {
/**
*
* Using the sort () method of Collections to sort the sort () method requires that multiple parameters be passed. One is the Collection to be sorted.
* The other is a Comparator, which is printed in two rows and can be compared accordingly.
*
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
List <provincevo> provincelist = new arraylist <provincevo> ();
Setprovincelist (provincelist );
For (provincevo key: provincelist ){
System. Out. Print (key. getname () + ",");
}
System. Out. println ();
Collections. Sort (provincelist, new pinyincomparator ());
For (provincevo key: provincelist ){
System. Out. Print (key. getname () + ",");
}
}

/**
* Prepare an object data set
*
* @ Param provincelist
*/
Private Static void setprovincelist (list <provincevo> provincelist ){
Provincevo = new provincevo ();
Provincevo. setname ("Nanchang City ");
ProvinceList. add (provinceVO );
ProvinceVO provinceVO1 = new ProvinceVO ();
ProvinceVO1.setName ("Jiujiang City ");
ProvinceList. add (provinceVO1 );
ProvinceVO = new ProvinceVO ();
ProvinceVO2.setName ("Xinyu City ");
ProvinceList. add (provincevo 2 );
ProvinceVO provinceVO3 = new ProvinceVO ();
ProvinceVO3.setName ("Yingtan City ");
ProvinceList. add (provinceVO3 );
ProvinceVO province_4 = new ProvinceVO ();
ProvinceVO4.setName ("Ganzhou City ");
ProvinceList. add (province_4 );
ProvinceVO provinceVO5 = new ProvinceVO ();
ProvinceVO5.setName ("Fuzhou ");
ProvinceList. add (provinceVO5 );
ProvinceVO provinceVO6 = new ProvinceVO ();
ProvinceVO6.setName ("Shangrao City ");
ProvinceList. add (provinceVO6 );
Provincevo provincevo7 = new provincevo ();
Provincevo7.setname ("Pingxiang City ");
Provincelist. Add (provincevo7 );
Provincevo provincevo8 = new provincevo ();
Provincevo8.setname ("Yichun City ");
Provincelist. Add (provincevo8 );
Provincevo provincevo9 = new provincevo ();
Provincevo9.setname ("Jingdezhen City ");
Provincelist. Add (provincevo9 );
Provincevo provincevo10 = new provincevo ();
Provincevo10.setname ("Ji 'an city ");
Provincelist. Add (provincevo10 );
}
}

The result shows the corresponding changes.

Nanchang city, Jiujiang city, Xinyu city, Yingtan City, Ganzhou City, Fuzhou City, Shangrao City, Pingxiang City, Yichun City, Jingdezhen city, Ji 'an city,
Fuzhou City, Ganzhou City, Ji 'an city, Jingdezhen city, Jiujiang city, Nanchang city, Pingxiang City, Shangrao City, Xinyu city, Yichun City, Yingtan City,

~~~~~~~~~~~~~~~~

The text is not good. I have a smile @~~ @..

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.