title: How to sort a list of objects from a parent object using a for loop Spring Mvc
body: <p>So I'm using Spring MVC and I'm trying to sort a <code>list</code> of objects that I'm getting from the parent object.  Right now I have a <code>Course</code> object that has a list of <code>Posts</code>.  I also have a <code>User</code> that's subscribed to <code>Courses</code> and I'm using a <code>for loop</code> to grab all the <code>posts</code> for each <code>Course</code> that the user is subscribed to.</p>
  
  <p>I want this to return in a news feed sort of way, and right now it's showing the first <code>post</code> at the top and new <code>posts</code> are added to the bottom.</p>
  
  <p>In other parts of my application I have a <code>findByObjectOrderbyIdDesc</code> which returns the objects in chronological order the way I want, but that's when I use a repository, and I'm not doing that here.  So I'm not sure how to do this.</p>
  
  <p>I'll show some of my code, so you guys can see what I'm talking about</p>
  
  <p>Controller</p>
  
  <pre><code>@RequestMapping(value="{user}", method=RequestMethod.GET)
  public String userPageGet (ModelMap model, @AuthenticationPrincipal User user)
  {
      List&lt;StudySet&gt; studySets = studySetRepo.findByUser(user);
      model.addAttribute("studySets", studySets);
  
      List&lt;Course&gt; courses = courseRepo.findByUser(user);
      model.addAttribute("courses", courses);
  
      Set&lt;Post&gt; posts = new TreeSet&lt;&gt;();
  
      for (Course course : courses) {
          posts.addAll(course.getPosts());
      }
      model.addAttribute("posts", posts);
  
      return "user";
  }
  </code></pre>
  
  <p>If anyone can see a solution to my issue and could let me know that would be great.  Thanks in advance.</p>