如题,一个List
怎么才能对它进行排序呢?
通过函数Collections.sort进行排列:
private String[] namelist = {"Id", "UserId", "UserNo", "Name", "TypeId", "Money", "Remark", "PassCode", "Order_no", "AddTime"};
private String[] keylist = {"id", "userid", "username", "name", "type", "money", "remark", "passcode", "ordernumber", "addtime"};
id是我map里面的字段,是自增的
Collections.sort(str, new Comparator, String>>() {
@Override
public int compare(Map, String> o1, Map, String> o2) {
String n1=o1.get("id");
String n2=o2.get("id");
// 我这里是从大到小的排序,如果从小到大,n1和n2换一下位置就可以了
return n2.compareTo(n1);
}
});
然后使用增强for循环遍历出整个map集合所有数据的
List, String>> list=new ArrayList, String>>();
for (Map,String> map:str){
list.add(map);
}
我这里的数据是要匹配到listview上的,所以添加到新的list之后,直接用适配器直接适配上去
public void doResults(List, String>> str) {
if (moneyList != null) {
moneyList.clear();
}
Collections.sort(str, new Comparator, String>>() {
@Override
public int compare(Map, String> o1, Map, String> o2) {
String n1=o1.get("id");
String n2=o2.get("id");
// 我这里是从大到小的排序,如果从小到大,n1和n2换一下位置就可以了
return n2.compareTo(n1);
}
});
List, String>> list=new ArrayList, String>>();
for (Map,String> map:str){
list.add(map);
}
moneyList.addAll(list);
adapter = new MoneyAdapter(MoneyChargeActivity.this, moneyList);
mXListView.setAdapter(adapter);