如何在Android中从Arraylist Date降序对日期进行排序?
问题内容:
我有一个ArrayList
存储Date
s的商店,并按降序对其进行了排序。现在,我要在中显示它们ListView
。这是我到目前为止所做的:
spndata.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
switch (position) {
case 0:
list = DBAdpter.requestUserData(assosiatetoken);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).lastModifiedDate != null) {
lv.setAdapter(new MyListAdapter(
getApplicationContext(), list));
}
}
break;
case 1:
list = DBAdpter.requestUserData(assosiatetoken);
Calendar c = Calendar.getInstance();
SimpleDateFormat df3 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate3 = df3.format(c.getTime());
Log.v("log_tag", "Date " + formattedDate3);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).submitDate != null) {
String sDate = list.get(i).submitDate;
SimpleDateFormat df4 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate4 = df4.format(sDate);
Map<Date, Integer> dateMap = new TreeMap<Date, Integer>(new Comparator<Date>(){
public int compare(Date formattedDate3, Date formattedDate4) {
return formattedDate3.compareTo(formattedDate4);
}
});
lv.setAdapter(new MyListAdapter(
getApplicationContext(), list));
}
}
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
问题答案:
创建Arraylist<Date>
Date类。并Collections.sort()
用于升序。
根据其元素的自然顺序将指定列表按升序排序。
对于按降序排序,请参见Collections.reverseOrder()
Collections.sort(yourList, Collections.reverseOrder());