gpt4 book ai didi

java - Java 中按日期顺序对 List 进行排序

转载 作者:行者123 更新时间:2023-12-01 16:49:30 25 4
gpt4 key购买 nike

我有一个以下形式的字符串数组列表

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);
}
}

但是由于它们位于字符串数组内,因此无法将其更改为日期格式,除非我更改它们然后将它们解析回字符串。

如有任何建议,我们将不胜感激。

最佳答案

答案是您应该将 String[] 转换为 AddressDateLocation 类数组。一旦你这样做了,按照你想要的方式对数据进行排序就会容易得多

public class AddressDateLocation implements Comparable<AddressDateLocation> {
Date date;
String address;
String location;

public void setDate(Date d) {}
public Date getDate() ...
public void setLocation(String loc) ...
public String getLocation() ...
public void setAddress(String addr) ...
public String getDate() ...

public int compareTo(AddressDateLocation other) ...
}

关于java - Java 中按日期顺序对 List<String[]> 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43353227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com