title: SpringData/Hibernate: Sort enum by it's field
body: <p>I have small problem with sorting enums by custom rule with springdata/hibernate.</p>
  
  <p>A have an enum called let's say: DeviceState, which has its own priority field.</p>
  
  <pre><code>public enum DeviceState {
      ON(4), OFF(3), UPDATE(2), CRITICAL(0), WARNING(1);
      private final int priority;
  
      private DeviceState(int priority) {
          this.priority = priority;
      }
  
      public int getPriority() {
          return priority;
      }
  }
  </code></pre>
  
  <p>The enum is used by DeviceEntity:</p>
  
  <pre><code>@Entity
  public class Device implements Serializable {
  
      // .... other fields
  
      @Enumerated(EnumType.STRING)
      private DeviceState state;
  
      // ....
  }
  </code></pre>
  
  <p>I use SpringData/Repository to get entities from database, I have used findAll(specification, pageable) method from org.springframework.data.repository.Respository class.</p>
  
  <pre><code>Page&lt;Device&gt; pagedDevices = deviceRepository.findAll(DeviceSpecifications.findByFilter(filter), pageable);
  </code></pre>
  
  <p>And if my request cointains sort=state,asc it will return the Devices sorted by state - alphabetically, but I would like to use priority field as sort criteria instead.</p>
  
  <p>What have i tried:
  change @Enumerated(STRING) to @Enumerated(ORIDINAL) - but this cannot happen  this is production working system with existing data, there are several devices which are using by external system and my project needs String values.</p>
  
  <p><a href="https://stackoverflow.com/questions/3605655/control-sort-order-of-hibernate-enumtype-string-properties">Control sort order of Hibernate EnumType.STRING properties</a> but i cannot add any new column since I already have production data and all enum values are persisted as String.</p>
  
  <p>I also have tried some "workarounds" with wrapping Page result - but these are just workarounds not pure solutions :)</p>
  
  <p>Any clue, help? idea?
  Please keep in mind that this is production working system and I cannot do any "fireworks".</p>