import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;
公共类测试器{
publicstaticvoidmain (string [ ] args ) {
ArrayList list=new ArrayList (;
list.add (东湾);
list.add (感到自豪);
list.add (东海湾-岩洞);
list.add (傲来药店);
/*
要使用collections sort (方法)对sort (方法)方法进行排序,必须传递参数。 一个是需要排序的Collection,另一个是Comparator
*/
collections.sort(list,new SpellComparator ) );
for(intI=0; i list.size (; I ) {
system.out.println(list.get(I ) );
}
}
}
//*
*汉字拼音顺序比较器
*/
classspellcomparatorimplementscomparator {
公共输入公司(Object o2,Object o2 ) {
try {
//取得比较对象的汉字代码,转换成字符串
stringS1=newstring(O1.tostring ).GetBytes )、(ISO-8859-1 ) );
stringS2=newstring(O2.tostring ).GetBytes )、(ISO-8859-1 ) );
String类的compareTo ) )方法比较两个对象
returnS1.comPareto(S2;
}catch(exceptione ) {
e .打印任务跟踪(;
}
返回0;
}
}