表是这样的,
id 类型 数据1 数据2
01 天 1 2
02 天 2 0
03 地 1 1
04 天 4 3
05 人 1 2
06 人 1 0
07 地 1 2
08 天 2 0把这样的数据放到一个ArrayList里,然后怎么先按类型再按ID给它排序啊,,用这个东西,Collections.sort
重写它也只能按一个字段排,,
id 类型 数据1 数据2
01 天 1 2
02 天 2 0
03 地 1 1
04 天 4 3
05 人 1 2
06 人 1 0
07 地 1 2
08 天 2 0把这样的数据放到一个ArrayList里,然后怎么先按类型再按ID给它排序啊,,用这个东西,Collections.sort
重写它也只能按一个字段排,,
用Collections.sort(Listlist, Comparator c) 这个方法.自己写一个Comparator.public class TestSort { public static void main(String args[]){
List aList = new ArrayList();
for(int i=0;i<10;i++){
Map aMap = new HashMap();
aMap.put("ID","0"+(i+1));
aMap.put("TYPE","ABC"+i);
aList.add(aMap);
}
Collections.sort(aList,new MyComparator());
}
}class MyComparator implements Comparator{
public MyComparator(){
super();
} public int compare(Object o1, Object o2) {
Map mapA =(Map)o1;
Map mapB =(Map)o2;
String idA=(String)mapA.get("ID");
String idB=(String)mapB.get("ID");
return idA.compareTo(idB);
}
}
经理帮我搞定,唉,JAVA这东西,还得学呀,,