title: Spring Data JPA, dynamic sorting based upon possible entry in nested collection
body: <p>As the codebase is already using Spring Data JPA, I would like to create a Sort
  object, which will be based upon the presence (or lack thereof) of a particular element 
  being present in a Collection, collection itself which is one of the element in the 
  primary table.  The Sort object's property would need to be dynamic, as a user might want to sort
  records once one way and the next time another way.</p>
  
  <p>Explicitly, if multiple PrimaryEntity objects have a SecondaryEntity with 'type' set to a particular
  value, I would then want to sort them based upon the corresponding 'notes' field in the corresponding
  SecondaryEntity.  Also, while I would want to retrieve all SecondaryEntity objects, I would want the 
  sorting to be based solely upon the SecondaryEntity records where 'type' is equal to, say, 'Important'.</p>
  
  <p>The classes look like the following (I also redefined 'equals' &amp; 'hashCode' for SecondaryEntity):</p>
  
  <pre><code>public class PrimaryEntity 
  {
  
  @OneToMany(mappedBy = "primary", cascade = CascadeType.ALL)
  @MapKey(name = "type")
  private Map&lt;String, SecondaryEntity&gt; myMap = new HashMap&lt;&gt;();
  
  @Column(name = "name")
  private String name;
  
  }
  
  public class SecondaryEntity 
  {
  
  @Column(name = "type", length = 200)
  private String type;
  
  @Column(name = "notes", length = 2000)
  private String notes;
  
  @ManyToOne
  @JoinColumn(name = "primary_id", referencedColumnName = "id")
  private PrimaryEntity primary;
  
   }
  </code></pre>
  
  <p>I would then want to create a Sort with a syntax similar to he following:</p>
  
  <p>Sort sort = new Sort("myMap[important].notes")</p>
  
  <p>Finally, while I am striving to sort the PrimaryEntity records as per above, it does not matter to 
  me how, for a given PrimaryEntity, its SecondaryEntity records are displayed.</p>
  
  <p>For example,</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;style&gt;
  table, th, td {
      border: 1px solid black;
      border-collapse: collapse;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;th&gt;Name&lt;/th&gt;
      &lt;th&gt;Type&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Second primary&lt;/td&gt;
      &lt;td&gt;Important&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Second primary&lt;/td&gt;
      &lt;td&gt;Other&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Second primary&lt;/td&gt;
      &lt;td&gt;Miscellaneous&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Third primary&lt;/td&gt;
      &lt;td&gt;Important&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;First primary&lt;/td&gt;
      &lt;td&gt;Important&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thank you.</p>