title: How to use natural sort with Spring Data Jpa
body: <p>I have a table column that I want to order on. The problem is that the column value contains both numbers and text. For example, the result is now ordered like this.</p>
  
  <pre><code>1. group one
  10. group ten
  11. group eleven
  2. group two
  </code></pre>
  
  <p>But I'd like the result to be ordered naturally, like this</p>
  
  <pre><code>1. group one
  2. group two
  10. group ten
  11. group eleven
  </code></pre>
  
  <p>When looking at the Spring configuration I can't seem to find an option that allows you to do this. I use the Spring Pageable class to set my order field and direction, with an additional use of JPA specifications. The method itself returns a Page with the first 20 results by default.</p>
  
  <p>I don't believe that Oracle supports Natural ordering out of the box so I have a stored procedure for that. Running the query using this procedure I get the desired result.</p>
  
  <pre><code>select ... from group order by NATURALSORT(group.name) asc
  </code></pre>
  
  <p>As you might expect I'd like to use that procedure by wrapping it around every ordered column that contains text. While maintaining to use pageables/pages and specifications. The research I done this far points me to a solution that might include</p>
  
  <ul>
  <li>Either creating and implementing a custom Specification</li>
  <li>Or extending the SimpleJpaRepository to change the way the Sort object is transformed.</li>
  </ul>
  
  <p>But I didn't seem to find a method that allows me to set the order using native SQL. The only way I found to set the order was by calling <code>orderBy</code> and including an <code>Order</code> object.</p>
  
  <p>So in general.</p>
  
  <ol>
  <li>Is there a way to globally enable natural ordering when using Spring Data Jpa, hibernate and the Oracle database</li>
  <li>If not, how can I wrap a single <code>order by column</code> with my stored procedure while still being able to use the <code>Page findAll(Pageable, Specifications)</code> method?</li>
  </ol>
  
  <p>Thanks in advance.</p>