0

I am trying to sort the following class:

public class Request {
    private String id;
    private String name;
    private List<RequestType> requestTypes;
}

public class RequestType {
    private String name;
    private String value;
    private RequestTypeEnum requestTypeEnum;
    private long date;
    private boolean done;
    private int priority;
    private String id;
}

The request class needs to be ordered based on the data from the requestTypes property. How would I go about approaching this problem

I started by doing nested for loops:

 for(int i= 0;  i< requests.size(); i++) {
        Request request = requests.get(i);
        for(int j = 0; j < request.getRequestTypes().size();j++) {

        }

    } 

Not sure what I need to do from there it needs to be ordered by the date field in ascending order.

  • 3
    Maybe looking at the Documentation site, and this example: [Sorting a List using Comparable or a Comparator](http://stackoverflow.com/documentation/java/3137/comparable-and-comparator/10693/sorting-a-list-using-comparablet-or-a-comparatort) will give a nudge in the right direction. – ppeterka Aug 12 '16 at 21:13
  • Simply use `Collections.sort(...)`, passing in the requestTypes list, and an appropriate Comparator. – Hovercraft Full Of Eels Aug 12 '16 at 21:14
  • 1
    `requestTypes.sort(Comparator.comparing(RequestType::getDate));` – Jorn Vernee Aug 12 '16 at 21:16
  • You first need to refine your requirements. Each request has 0 to N request types, each having a date. So you need to define how to sort: basd on the lowest date in the lists, on the greater date, on something else? What if a request has no request type, and thus no date? – JB Nizet Aug 12 '16 at 21:22
  • it requires atleast one requesttype or else a request won't exist. – yetanotherposter Aug 12 '16 at 21:27

0 Answers0