按日期排序列表<string []>在Java

时间:2017-04-11 18:06:02

标签: java android list sorting arraylist

我有一个表单

的String数组列表
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数组中,所以除非我更改它们然后将它们解析回字符串,否则它们不能更改为日期格式。

任何建议都将不胜感激。

4 个答案:

答案 0 :(得分:2)

答案是你应该将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) ...
}

答案 1 :(得分:1)

您需要的数据结构可能不是String的数组,除非这些部分的顺序最终在公共API中。您最好将信息保留在具有Sub LastRowInOneColumn() Dim LastRow As Long Dim i As Long, j As Long 'Find the last used row in a Column: column A in this example With Worksheets("Sheet1") LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With 'MsgBox (LastRow) 'first row number where you need to paste values in Sheet1' With Worksheets("Sheet2") j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 End With For i = 1 To LastRow With Worksheets("Sheet1") If .Cells(i, 1).Value = "X" Then .Rows(i).Copy Destination:=Worksheets("Sheet2").Range("A" & j) j = j + 1 End If End With Next i End Sub 语义的本地JSONObject中。然后你可以简单地按时间戳排序:

Map

注意:问题中建议的日期格式可以使用文本表示法比较时间戳。

答案 2 :(得分:1)

您可以将日期转换为LocalDateTime个对象,并使用compareTo方法。

答案 3 :(得分:1)

我会使用List#sort函数,如下所示:

currentLoadAddressLocations.sort((sa1, sa2) -> { // sa = String Array
    try {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        return new SimpleDateFormat(pattern, Locale.US).parse(sa1[0])
                .compareTo(new SimpleDateFormat(pattern, Locale.US).parse(sa2[0]));
    } catch (ParseException e) {
        // If your dates are all valid you shouldn't come here.
        e.printStackTrace();
        return -1; // move all parse ParseExceptions to the first positions
    }
});
相关问题