title: Duplicates when sorting using Pageable
body: <p>Controller:</p>
  
  <pre><code>@RequestMapping(path = "/serviceslist", method = RequestMethod.GET)
      public Page&lt;ServiceResponse&gt; getServicesList(
              @RequestParam(defaultValue = "0") Integer page,
              @RequestParam(defaultValue = "10") Integer size,
              @RequestParam(required = false) String search,
              @RequestParam(required = false) String name,
              @RequestParam(required = false) String jobs,
              @RequestParam(required = false) Boolean needsPatrol,
              @RequestParam(defaultValue = "createTime") String sort,
              @RequestParam(defaultValue = "asc") String sortDir
      ) {
  
          ServiceListRequest request = new ServiceListRequest(search, name, jobs, needsPatrol);
  
          Sort.Direction direction;
  
          if (sortDir.equals("asc")) {
              direction = Sort.Direction.ASC;
          } else {
              direction = Sort.Direction.DESC;
          }
  
          return serviceService.getServicesList(request, of(page, size, direction, sort))
                  .map(ServiceResponse::new);
      }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>@Entity
  @Data
  @Table(name = "service")
  public class Service {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(unique = true, nullable = false, columnDefinition = "serial")
      private Long id;
  
      @Column(name = "name", nullable = false)
      private String name;
  
      @Column(name = "price", nullable = false)
      private float price;
  
      @Column(name = "season", nullable = false, columnDefinition = "season_enum")
      @Enumerated(EnumType.STRING)
      @Type(type = "pgsql_enum")
      private SeasonEnum season;
  
      @Column(name = "explanation")
      private String explanation;
  
      @Column(name = "needs_patrol")
      private boolean needsPatrol;
  
      @Column(name = "image_required")
      private boolean imageRequired;
  
      @ManyToOne
      @JoinColumn(name = "color_id", nullable = false)
      private Color color;
  
      @Column(name = "start_date")
      private Date startDate;
  
      @Column(name = "end_date")
      private Date endDate;
  
      @OneToMany(mappedBy = "service")
      @JsonIgnore
      private List&lt;ObjectService&gt; objectServices = new ArrayList&lt;&gt;();
  
      @ManyToMany(fetch = FetchType.EAGER, targetEntity = Job.class)
      @JoinTable(name = "service_has_job",
              joinColumns = { @JoinColumn(name = "service_id", referencedColumnName = "id")},
              inverseJoinColumns = { @JoinColumn(name = "job_id", referencedColumnName = "id")}
      )
      private Set&lt;Job&gt; jobs = new HashSet&lt;&gt;();
  
      @Column(name = "create_time", nullable = false, updatable = false)
      @JsonIgnore
      private Date createTime = new Date();
  
      @Column(name = "update_time", nullable = false)
      @JsonIgnore
      private Date updateTime = new Date();
  
      @PreUpdate
      @PrePersist
      public void onCreateOnUpdate() {
          updateTime = new Date();
      }
  
      public enum SeasonEnum {
          winter, summer, all_year
      }
  }
  </code></pre>
  
  <p>A service can have many jobs. </p>
  
  <p>What I'm trying to achieve is sort ASC/DESC by the first item in jobs, but currently, if a service has lets say 4 jobs, after sorting using:</p>
  
  <pre><code>localhost:8080/serviceslist?sort=jobs&amp;sortDir=asc
  </code></pre>
  
  <p>it gives me the service 4 times, but I need it to be distinct. Does pageable have a way to remove duplicates and fix my sorting problem?</p>
  
  <p>ServiceService getServicesList function:</p>
  
  <pre><code>public Page&lt;com.bitweb.syda.data.entity.service.Service&gt; getServicesList(ServiceListRequest request, Pageable pageable) {
          Specification&lt;com.bitweb.syda.data.entity.service.Service&gt; spec = where(null);
  
          if (request.getSearch() != null) spec = spec.and(search(request.getSearch()));
          if (request.getName() != null) spec = spec.and(name(request.getName()));
          if (request.getJobs() != null) spec = spec.and(hasJobs(request.getJobs()));
          if (request.getNeedsPatrol() != null) spec = spec.and(needsPatrol(request.getNeedsPatrol()));
  
          return serviceRepository.findAll(spec, pageable);
      }
  </code></pre>
  
  <p>Sorting by name and stuff works fine, just the jobs part is broken :/</p>
  
  <p>Edit 21.04.2019: I'm sorry if I wasn't really clear before. The ideal result would be to sort by the first job alphabetically, since jobs are in order of when theyre done. Is that possible in any way? Thanks in advance!</p>
  
  <p>Job entity: </p>
  
  <pre><code>@Entity
  @Data
  @Table(name = "job")
  public class Job {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(unique = true, nullable = false, columnDefinition = "serial")
      private Long id;
  
      @Column(name = "name", nullable = false)
      private String name;
  
      @Column(name = "explanation")
      private String explanation;
  
      @ManyToMany(fetch = FetchType.EAGER, targetEntity = Competence.class)
      @JoinTable(name = "job_has_competence",
              joinColumns = { @JoinColumn(name = "job_id", referencedColumnName = "id")},
              inverseJoinColumns = { @JoinColumn(name = "competence_id", referencedColumnName = "id")}
      )
      private Set&lt;Competence&gt; competences = new HashSet&lt;&gt;();
  
      @ManyToMany(fetch = FetchType.EAGER, targetEntity = Material.class)
      @JoinTable(name = "job_has_material",
              joinColumns = { @JoinColumn(name = "job_id", referencedColumnName = "id")},
              inverseJoinColumns = { @JoinColumn(name = "material_id", referencedColumnName = "id")}
      )
      private Set&lt;Material&gt; materials = new HashSet&lt;&gt;();
  
      @ManyToMany(fetch = FetchType.EAGER, targetEntity = Mechanism.class)
      @JoinTable(name = "job_has_mechanism",
              joinColumns = { @JoinColumn(name = "job_id", referencedColumnName = "id")},
              inverseJoinColumns = { @JoinColumn(name = "mechanism_id", referencedColumnName = "id")}
      )
      private Set&lt;Mechanism&gt; mechanisms = new HashSet&lt;&gt;();
  
      @Column(name = "create_time", nullable = false, updatable = false)
      @JsonIgnore
      private Date createTime = new Date();
  
      @Column(name = "update_time", nullable = false)
      @JsonIgnore
      private Date updateTime = new Date();
  
      @PreUpdate
      @PrePersist
      public void onCreateOnUpdate() {
          updateTime = new Date();
      }
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/qUhOn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qUhOn.png" alt="Pic"></a></p>