R
R
renniqs2021-07-15 17:58:42
Java
renniqs, 2021-07-15 17:58:42

How to sort a list of objects by field in another list of objects?

Help with sorting, you need to sort the list by the field of the object that is in another list
Here is an example structure, and you need to sort by value and a certain guid in Priority

class ResponseData {

  private User user;
  private List<Event> event;

  public static User {
     private String name;	   
     private String email;
  }

  public static class Event {
     private String guid;
     private List<Priority> priority;
  }

  public static class Priority {
    private String guid;
    private String name;
    private String value;
  }

}


Input example:
[
  {
    "user": {
      "name": "Bob",
      "email": "[email protected]"
    },
    "event": [
      {
        "guid": "1",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "35.29"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "25.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "85.98"
          }
        ]
      },
      {
        "guid": "2",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "14.19"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "75.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "75.98"
          }
        ]
      },
      {
        "guid": "3",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "95.29"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "29.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "65.98"
          }
        ]
      },
      {
        "guid": "4",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "99.29"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "19.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "85.98"
          }
        ]
      },
      {
        "guid": "5",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "97.29"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "39.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "95.98"
          }
        ]
      }
    ]
  }
]

Next, you need to sort the event list by value in priority where guid is sport

, as a result we get that the list of events will be 2, 1, 3, 5, 4

Here is an example of sorting:

[
  {
    "user": {
      "name": "Bob",
      "email": "[email protected]"
    },
    "event": [
      {
        "guid": "2",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "14.19"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "75.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "75.98"
          }
        ]
      },
      {
        "guid": "1",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "35.29"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "25.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "85.98"
          }
        ]
      },
      {
        "guid": "3",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "95.29"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "29.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "65.98"
          }
        ]
      },
      {
        "guid": "5",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "97.29"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "39.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "95.98"
          }
        ]
      },
      {
        "guid": "4",
        "priority": [
          {
            "guid": "sport",
            "name": "Sport",
            "value": "99.29"
          },
          {
            "guid": "work",
            "name": "work",
            "value": "19.19"
          },
          {
            "guid": "hobby",
            "name": "hobby",
            "value": "85.98"
          }
        ]
      }
    ]
  }
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-07-15
@renniqs

Implement Comparable or Comparator, then standard sorting tools.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question