这里我的jsonArray数据如下:
[{"LeadId":4,
"CoreLeadId":0,
"CompanyId":7,
"AccountNo":"5675",
"ScheduleOn":"2015-05-11T00:00:00"},
{"LeadId":7,
"CoreLeadId":2,
"CompanyId":8,
"AccountNo":"sample string 4",
"ScheduleOn":"2015-12-01T15:04:23.217"}]
我想按日期和时间(ScheduleOn)排序并放入listview。在我身边,我发送我的代码的snnipt,我设置适配器。我们可以排序到listItemService。请帮帮我。
JSONArray jsonArray = dpsFunctionFlow.getAllServiceDetail("1");
listItemService = new Gson().fromJson(jsonArray.toString(),
new TypeToken<List<AppointmentInfoDto>>() {
}.getType());
mAdapter = new AdapterAppointment(getActivity(), listItemService);
listView.setAdapter(mAdapter);
答案 0 :(得分:1)
您应该可以使用Collections.sort(...)
传递Comparator
来比较2 AppointmentInfoDto
个对象。
Collections.sort(listItemService, new Comparator<AppointmentInfoDto>() {
@Override public int compare(AppointmentInfoDto l, AppointmentInfoDto r) {
// Compare l.ScheduleOn and r.ScheduleOn
}
}
答案 1 :(得分:0)
///按日期任意键排序JSON
public static JSONArray sortJsonArray(JSONArray数组,final String key,final boolean isCase){
列出jsonsList = new ArrayList();
试试{
for(int i = 0; i&lt; array.length(); i ++){
jsonsList.add(array.getJSONObject(I));
}
Collections.sort(jsonsList,new Comparator(){
@覆盖
public int compare(JSONObject v_1,JSONObject v_2){
String CompareString1 =“”,CompareString2 =“”;
试试{
CompareString1 = v_1.getString(key); //密钥必须出现在JSON中
CompareString2 = v_2.getString(key); //密钥必须出现在JSON中
} catch(JSONException ex){
// Json Excpetion handling
}
返回CompareString1.compareTo(CompareString2);
}
});
} catch(JSONException ex){
// Json Excpetion handling
}
返回新的JSONArray(jsonsList);
}
// _对于任何字符串键值,使用JSON .........
public static JSONArray sortJsonArray(JSONArray数组,final String key,final boolean isCase){
列出jsonsList = new ArrayList();
试试{
for(int i = 0; i&lt; array.length(); i ++){
jsonsList.add(array.getJSONObject(I));
}
Collections.sort(jsonsList,new Comparator(){
@覆盖
public int compare(JSONObject v_1,JSONObject v_2){
String CompareString1 =“”,CompareString2 =“”;
试试{
CompareString1 = v_1.getString(key); //密钥必须出现在JSON中
CompareString2 = v_2.getString(key); //密钥必须出现在JSON中
} catch(JSONException ex){
// Json Excpetion handling
}
return isCase? CompareString1.compareTo(CompareString2):CompareString1.compareToIgnoreCase(CompareString2);
}
});
} catch(JSONException ex){
// Json Excpetion handling
}
返回新的JSONArray(jsonsList);
}