title: spring data sort by map-value within a given key
body: <p>I would like to sort a Map of  by the value. For example I have <strong>Person</strong> class which has a map of <strong>details</strong> that are stored in a map with key-value <code>Map&lt;String, String&gt;</code>.</p>
  
  <p>I am using springboot with hibernate5. This is the mapping.</p>
  
  <pre><code>public class Person implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @DocumentId
      @Column(name = "personid")
      private Integer id;
  
      @Column(name = "name")
      private String name;
  
      // this is a collection of person details
      @ElementCollection()
      @MapKeyColumn(name = "detailkey")
      @Column(name = "detailvalue")
      @CollectionTable(name = "details", joinColumns = @JoinColumn(name = "personid"))
      Map&lt;String, String&gt; details = new HashMap&lt;&gt;();
  
      //getter and setters omitted
  </code></pre>
  
  <p>So far I am able to retrieve a person with some specific detailskey and specific detail value. So for example a person table in the DB has <strong>eyecolor</strong> as detail attribute and as value can have "<strong>green</strong>", "<strong>blue</strong>", "<strong>brown</strong>". Note this is not a real example, just for clarity purposes.</p>
  
  <p>So for example I can get the list of persons and sort them by their name, in the controller I can do</p>
  
  <pre><code>Sort sort = new Sort(Sort.Direction.ASC, "name");
  </code></pre>
  
  <p>and the opposite direction</p>
  
  <pre><code>Sort sort = new Sort(Sort.Direction.DESC, "name");
  Pageable pageable = new PageRequest(1, 10, sort);
  pageResult = personRepository.findAll(
              "eyecolor", "green", pageable
          );
  </code></pre>
  
  <p>and this one will return the list of persons that have "eyecolor" as green. So far so good and this is working as expected. Now I would like to define a sorting on the detailvalue. </p>
  
  <p>For example I would like to get a list of person sorted by their eyecolor. So first I should have the persons that have "<strong>blue</strong>", "<strong>brown</strong>", "<strong>green</strong>".</p>
  
  <p>how can the Sort be specified in this case ? </p>
  
  <p>In standard SQL it would be something like this:</p>
  
  <p>SELECT p.* from persons p LEFT JOIN details d ON
    p.personid = d.personid AND p.detailkey='eyercolor' ORDER BY 
    p.detailvalue ASC;</p>