Sorting by some of the fields in Json Object

前端 未结 2 1873
孤街浪徒
孤街浪徒 2021-01-20 23:21

I have a Josn file containing array of objects like :

{
    \"tId\": \"Something\",
    \"StartTime\": \"05/29/2013 5:28:33 PM\",
    \"CompleteTime\": \"05/29/         


        
相关标签:
2条回答
  • 2021-01-20 23:36

    You should create a class MyObject to hold a data of one object (tId, startTime, completeTime, status, machineName).

    Then, parse each JsonObject to MyObject and add it to a List<MyObject>.

    Then, use Collections.sort() and a comparator to sort your list.

    0 讨论(0)
  • 2021-01-20 23:45

    Your sorting routine will look something like this, depending on the specifics of how you do your JSON/Object mapping:

    Collections.sort(myObjectList, new Comparator<MyObject>() {
      @Override
      int compare(MyObject a, MyObject b) {
        if (a.getStartTime().equals(b.getStartTime())) {
          return a.getMachineName().compare(b.getMachineName());
        }
        return a.getStartTime().compare(b.getStartTime());
      }
    });
    
    0 讨论(0)
提交回复
热议问题