In the first case:
- Comparator cmp = collator.getinstance (Java.util.Locale.CHINA);
- String[] arr = {"John", "Dick", "Harry", "Liu Liu"};
- Arrays.sort (arr, CMP);
- for (int i = 0; i < arr.length; i++)
- System.out.println (Arr[i]);
In the second case:
Comparablebean.java
Import Java.text.CollationKey;
Import Java.text.Collator;
Import Java.text.RuleBasedCollator;
Import Java.util.Comparator;
public class comparablebean{
private String name;
Public Comparablebean (String name) {
THIS.name = name;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
}
Class Comparablebeancomparator implements Comparator//<comparablebean>
{
RuleBasedCollator collator; can set your rules for the instance "Collator"
Public Comparablebeancomparator ()
{
Collator = (rulebasedcollator) collator.getinstance (Java.util.Locale.CHINA);//Try testing various locales
}
public int Compare (object Obj1, Object obj2) {
String tempname1 = ((Comparablebean) obj1). GetName ();
String tempname2 = ((Comparablebean) obj2). GetName ();
Collationkey C1 = Collator.getcollationkey (TEMPNAME1);
Collationkey C2 = Collator.getcollationkey (tempname2);
Return Collator.compare ((Collationkey) C1). Getsourcestring (),
((Collationkey) c2). getsourcestring ());
Return Collator.compare ((collationkey) c2). Getsourcestring (),
((Collationkey) C1). Getsourcestring ());
}
public int Compare (Comparablebean obj1, Comparablebean obj2) {
String tempname1 = Obj1.getname ();
String tempname2 = Obj2.getname ();
//
Collationkey C1 = Collator.getcollationkey (TEMPNAME1);
Collationkey C2 = Collator.getcollationkey (tempname2);
Return Collator.compare ((Collationkey) C1). Getsourcestring (),
((Collationkey) c2). getsourcestring ());
// }
}
The end of Comparablebean.java
Test code:
Comparablebean[] Namecontent = {New Comparablebean ("Everything starts from the actual"),
New Comparablebean ("Invincible"), new Comparablebean ("polynomial"),
New Comparablebean ("implement"), New Comparablebean ("Close to the masses"),
New Comparablebean ("Cardinal"), New Comparablebean ("gnashing of Teeth"),
New Comparablebean ("respectfully"), new Comparablebean ("civilian police"),
New Comparablebean ("Business Contract Responsibility System")};
Arrays.sort (Namecontent,new comparablebeancomparator ());
for (int i = 0; i < namecontent.length; i++) {
System.out.println (Namecontent[i].getname ());
}