【发布时间】:2017-09-07 06:31:06
【问题描述】:
我有一个形式为字符串数组的列表
List<String[]> currentLoadAddressLocations = new ArrayList<>();
通过 JSONObject 数组设置和解析
try {
JSONObject dataObject = new JSONObject(data);
JSONArray dataObjArray = dataObject.getJSONArray("stops");
Log.i(TAG, dataObjArray.toString());
for (int i = 0; i < dataObjArray.length(); i++) {
JSONObject addressAndLocation = dataObjArray.getJSONObject(i);
addressGPointJSON.add(addressAndLocation.getString("address"));
locationGPointJSON.add(addressAndLocation.getString("location"));
cityGPointJSON.add(addressAndLocation.getString("city"));
stateGPointJSON.add(addressAndLocation.getString("state"));
fromGPointJSON.add(addressAndLocation.getString("from"));
latGPointJSON.add(reverseGeocoderLatLong(addressGPointJSON.get(i) + ", " + cityGPointJSON.get(i) + ", " + stateGPointJSON.get(0), true));
longGPointJSON.add(reverseGeocoderLatLong(addressGPointJSON.get(i) + ", " + cityGPointJSON.get(i) + ", " + stateGPointJSON.get(0), false));
currentLoadAddressLocations.add(i,
new String[]{
fromGPointJSON.get(i),
addressGPointJSON.get(i),
locationGPointJSON.get(i),
cityGPointJSON.get(i),
stateGPointJSON.get(i),
latGPointJSON.get(i),
longGPointJSON.get(i)
});
} // end of for loop
} // end of try catch block
catch(JSONException e){
e.printStackTrace();
}
目前,代码返回了我需要的数据结构,一个 String[] 形式
["2017-03-30 21:00:00", "Address example 123", "Location Example", "CITY", "STATE", "lat", "long"]
根据返回的 JSON 对象中的停靠点数重复。我需要找到一种方法来按时间在 currentLoadAddressLocations 数组中从上到下对数组的第一个值进行排序,所以如果其中一个日期是“2017-03-30 15:00:00”,另一个是“2017” -03-30 14:00:00" 然后前面的那个优先并将日期移动到 currenLoadAddressLocations 数组的顶部,同时将第二个放在下面。我很难找到这样做的方法。目前我知道如果我将日期解析为可以比较日期:
Date from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).parse("2017-03-30 21:00:00");
但不知道如何在循环通过 currentLoadAddressLocations 列表时解决此问题。此外,我不知道如何访问这些值以比较它们。我可以通过使用循环选择
for(String[] array: currentLoadAddressLocations){
for(String s: array){
Log.i(TAG,"ITEM: " + s);
}
}
但由于它们位于字符串数组中,除非我更改它们然后将它们解析回字符串,否则它们无法更改为日期格式。
任何建议将不胜感激。
【问题讨论】:
标签: java android list sorting arraylist