泛型list中汉字按首字母排序 20
当有一个bank类,里面有2个属性:bankname,bankcode对应属性的是get,set方法。Bankbank1=newBank();Bankbank2=newB...
当有一个bank类,里面有2个属性:bankname,bankcode对应属性的是get,set方法。
Bank bank1=new Bank ();
Bank bank2=new Bank ();
Bank bank3=new Bank ();
Bank bank4=new Bank ();
bank1.setBankname("浙商银行);
bank1.setBankcode("316290088001");
bank2.setBankname("上海农商银行");
bank2.setBankcode("402290088001");
bank3.setBankname("农业银行");
bank3.setBankcode("103290088001");
bank4.setBankname("中国工商银行");
bank4.setBankcode("102290088001");
List<Bank > list = new ArrayList<Bank >();
list .add(bank1);
list .add(bank2);
list .add(bank3);
list .add(bank4);
现在希望list里按bankname里面的首字母排序,哪位大神帮帮忙。。。 展开
Bank bank1=new Bank ();
Bank bank2=new Bank ();
Bank bank3=new Bank ();
Bank bank4=new Bank ();
bank1.setBankname("浙商银行);
bank1.setBankcode("316290088001");
bank2.setBankname("上海农商银行");
bank2.setBankcode("402290088001");
bank3.setBankname("农业银行");
bank3.setBankcode("103290088001");
bank4.setBankname("中国工商银行");
bank4.setBankcode("102290088001");
List<Bank > list = new ArrayList<Bank >();
list .add(bank1);
list .add(bank2);
list .add(bank3);
list .add(bank4);
现在希望list里按bankname里面的首字母排序,哪位大神帮帮忙。。。 展开
3个回答
展开全部
首先拼音排序需要用到pinyin4j的jar
有一个转换为字母的方法PinyinHelper.toHanyuPinyinStringArray
实现排序两种方式
Bank类实现Comparable接口重写其compareTo方法 调用Collections.sort(list);即可
传入自定义比较器(类似如下写法)
Collections.sort(list,, new Comparator<Bank>(){
public int compare(Bank o1, Bank o2) {
return null;
}
})
展开全部
import java.util.ArrayList;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import sun.org.mozilla.javascript.internal.NativeArray;
import sun.org.mozilla.javascript.internal.NativeObject;
public class Test6
{
public static void main(String[] args)
{
Bank bank1 = new Bank();
Bank bank2 = new Bank();
Bank bank3 = new Bank();
Bank bank4 = new Bank();
bank1.setBankname("浙商银行");
bank1.setBankcode("316290088001");
bank2.setBankname("上海农商银行");
bank2.setBankcode("402290088001");
bank3.setBankname("农业银行");
bank3.setBankcode("103290088001");
bank4.setBankname("中国工商银行");
bank4.setBankcode("102290088001");
ArrayList<Bank> list = new ArrayList<Bank>();
list.add(bank1);
list.add(bank2);
list.add(bank3);
list.add(bank4);
System.out.println(list);
String jsonstr = list.toString();
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("js");
String fun = "var arr="
+ jsonstr
+ ";arr.sort(function(a,b){var an = a.bankname,bn=b.bankname;if(an.localeCompare(bn)>0){return 1;}else if(an.localeCompare(bn)<0){return -1;}else{return 0;}});";
try
{
NativeArray na = (NativeArray) se.eval(fun);
for(int i = 0; i < na.getLength(); i++)
{
NativeObject object = (NativeObject) na.get(i, null);
Object[] ids = object.getIds();
Bank bank = new Bank();
bank.setBankname(object.get(ids[0].toString(), null).toString());
bank.setBankcode(object.get(ids[1].toString(), null).toString());
list.set(i, bank);
}
}
catch(ScriptException e)
{
e.printStackTrace();
}
System.out.println(list);
}
}
class Bank
{
private String bankname;
private String bankcode;
public String getBankname()
{
return bankname;
}
public void setBankname(String bankname)
{
this.bankname = bankname;
}
public String getBankcode()
{
return bankcode;
}
public void setBankcode(String bankcode)
{
this.bankcode = bankcode;
}
@Override
public String toString()
{
return String.format("{\"bankname\":\"%s\", \"bankcode\":\"%s\"}", bankname, bankcode);
}
}
追问
这个可以实现吗?
追答
可以哦,都写出来了哦,打印了哦,你明知故问哦,什么意思啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Collections.sort(list, new Sort()); 自定义排序
排序实现
class Sort implements Comparator<Bank> {
public int compare(Bank o1, Bank o2) {
char c1 = o1.getBankname().charAt(0);
char c2 = o2.getBankname().charAt(0);
return concatPinyinStringArray(net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(c1)).compareTo(concatPinyinStringArray(net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(c2)));
}
}
排序实现
class Sort implements Comparator<Bank> {
public int compare(Bank o1, Bank o2) {
char c1 = o1.getBankname().charAt(0);
char c2 = o2.getBankname().charAt(0);
return concatPinyinStringArray(net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(c1)).compareTo(concatPinyinStringArray(net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(c2)));
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询