0

I have a DTO class I need to sort, it looks like this:

public class MessagesWithComments {    
     private Message parentMessage;
     private List<Message> childMessages;
}

My Message class:

public class Message {    
     private String message;
     private Date createdDate;
}

My service then returns a list of MessagesWithComments.

public List<MessagesWithComments> getParentsWithComments(....) {....}

I need these to be ordered so that the most recent message date of each MessageWithComments comes first, the tricky part is the date can be inside the parentMessage or inside the list of childMessages. So for example:

MessageWithComments 1

parentMessage date = '2016-10-13 10:40pm'
   no child messages

MessageWithComments 2

parentMessage date = '2016-10-10 10:40pm'
   childMessage date = '2016-10-11 12:31pm'
   childMessage date = '2016-10-14 11:21pm'

MessageWithComments 3

parentMessage date = '2016-10-11 10:40am'
   childMessage date = '2016-10-12 12:31pm'
   childMessage date = '2016-10-13 10:28pm'

So in this example I would have my list of MessagesWithComments return in the order:

2(most recent date of '2016-10-14 11:21pm'), 
1(most recent date of '2016-10-13 10:40pm'), 
3(most recent date of 2016-10-13 10:28pm') 

Is there a standard way of doing this sort of thing or should I add another variable to my MessageWithComments class called 'mostRecentMessageDate' and then sort the list that way?

3
  • 1
    How about using some custom comparator? Commented Oct 14, 2016 at 16:35
  • Use custom Comparator for this. Commented Oct 14, 2016 at 16:35
  • Thanks for the response @RC and @Victor Sorokin but how would I do this? I know I can compare the dates and sort my sub object list childMessages or I could sort my list of MessagesWithComments based off the parentMessage date, but how do I implement the comparator that checks the parentMessage date and all the dates in the list of childMessages so that it can tell me the most recent date in side of each MessageWithComments object ? Commented Oct 14, 2016 at 16:44

2 Answers 2

1

I suggest using a custom Comparator.

You can implement your Comparator by defining how to compare two MessagesWithComments objects, and then use the Java Collections class to sort.

class MessagesWithCommentsComparator implements Comparator<MessagesWithComments> {

    @Override
    public int compare(MessagesWithComments o1, MessagesWithComments o2) {
        // parse the MessagesWithComments objects to determine order
    }
}

Then you can use the Collections class:

Collections.sort(messagesWithCommentsList, new MessagesWithCommentsComparator());

By defining how to compare two MessagesWithComments objects in the MessagesWithCommentsComparator, the Collections class then knows how to sort by using the compare() method that you've overridden.

Sign up to request clarification or add additional context in comments.

Comments

1

As some comments have already pointed out you could use a Comparator. Another option is to implement the Comparable interface. Here's some good info regarding the difference

1 Comment

Wow, that's really neat. I tried to answer but it became too complicated. this is so much simpler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.