急问一个关于list的排序问题,高手请进!!
不知道怎么描述,例如如下的例子:
List familylist=new ArrayList();
FamilyModel family1=new FamilyModel();
family1.setId= "01 ";
family1.setName= "老大 ";
family1.setChiName= "老大儿子1 ";
familylist.add(family1);
FamilyModel family2=new FamilyModel();
family2.setId= "02 ";
family2.setName= "老二 ";
family2.setChiName= "老二儿子1 ";
familylist.add(family2);
FamilyModel family3=new FamilyModel();
family3.setId= "03 ";
family3.setName= "老大 ";
family3.setChiName= "老大儿子2 ";
familylist.add(family3);
……
这样可以得到一个放着FamilyModel的familylist,但我希望这个familylist能按FamilyModel里的Name来排序(最起码要能把“老大”的FamilyModel集中放在一起,“老二”的也放在一起)不知道应该怎样做比较简便呢?
------解决方案--------------------例子:
http://dev.csdn.net/article/31/31142.shtm
------解决方案--------------------Collections.sort(familylist, new Comparator <FamilyModel> () {
public int compare(FamilyModel o1, FamilyModel o2) {
if(o1.getName().compareTo(o2.getName()) != 0){
return o1.getName().compareTo(o2.getName());
}else{
return o1.getChiName().compareTo(o2.getChiName());
}
}
});
------解决方案--------------------学习之。
------解决方案--------------------为什么不将ChiName类型设为String[]呢?