{"id":5051,"date":"2012-02-02T13:18:06","date_gmt":"2012-02-02T07:48:06","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5051"},"modified":"2017-09-22T15:32:39","modified_gmt":"2017-09-22T10:02:39","slug":"alphanum-sort","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/alphanum-sort\/","title":{"rendered":"Alphanumeric Sorting using Criteria Query (with MySQL database)"},"content":{"rendered":"<p>I am working on a Grails application with MySQL database. I\u00a0had a use case in which I had to implement alphanumeric sorting using Criteria Query on Grails. By alphanumeric sorting I mean if there is a class Employee with field empId then on doing :<\/p>\n<p>[plain]<br \/>\nEmployee e1 = new Employee(empId:&#8217;emp10&#8242;).save()<br \/>\nEmployee e2 = new Employee(empId:&#8217;emp2&#8242;).save()<br \/>\nEmployee e3 = new Employee(empId:&#8217;emp1&#8242;).save()<br \/>\nEmployee e4 = new Employee(empId:&#8217;1emp&#8217;).save()<br \/>\nEmployee e5 = new Employee(empId:&#8217;10emp&#8217;).save()<br \/>\nEmployee e6 = new Employee(empId:&#8217;2emp&#8217;).save()<\/p>\n<p>Employee.createCriteria().list([sort:&#8217;empId&#8217;])<br \/>\n            \/* OR *\/<br \/>\nEmployee.createCriteria().list{<br \/>\n    order(&#8217;empId&#8217;)<br \/>\n}<br \/>\n[\/plain]<\/p>\n<p><strong>should give result like [e4,e6,e5,e3,e2,e1]<\/strong>. But according to sql sorting the <strong>result would be [e5,e4,e6,e3,e1,e2]<\/strong><\/p>\n<p>One thing to note was that criteria query implemented sorting at the database end.<br \/>\nAfter googling around, I came <a href=\"http:\/\/drupal.org\/project\/natsort\">across a script<\/a> that had a SQL function which converted the field value to string that can alphanumerically sorted easily at database end.<\/p>\n<p>To migrate the functions in script to database you have to unzip the script and write following command on terminal:<\/p>\n<p>[shell]mysql -u username -p database_name &lt; \/path\/to\/unzipped\/directory\/natsort.install.mysql[\/shell]<\/p>\n<p>Now only thing I had to do was to get a derived field from the Grails property that had to be sorted alphanumerically which was easily possible as the Grails had ability to create transient field using formula (thanks to wonderful <a href=\"http:\/\/www.tothenew.com\/blog\/create-transients-using-formula-in-grails\/\">Blog by Uday<\/a>). Thus I had to create a new transient field using SQL function in formula. So I did something like this :<\/p>\n<p>[groovy]<br \/>\nclass Employee {<br \/>\n    String empId<br \/>\n    String empIdSortField \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/transient field<br \/>\n    static mapping = {<br \/>\n        empIdSortField formula: &#8216;natsort_canon(empId, &quot;natural&quot;)&#8217;<br \/>\n    }<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>Now if I had to get Employee objects are sorted by empId alphanumerically, what I would do is :<\/p>\n<p>[groovy]<br \/>\nEmployee.createCriteria().list {<br \/>\n    order(&#8217;empIdSortField&#8217;)<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>Hope this was helpful to you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am working on a Grails application with MySQL database. I\u00a0had a use case in which I had to implement alphanumeric sorting using Criteria Query on Grails. By alphanumeric sorting I mean if there is a class Employee with field empId then on doing : [plain] Employee e1 = new Employee(empId:&#8217;emp10&#8242;).save() Employee e2 = new [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":20},"categories":[7],"tags":[761,269,763,29,4840,762,76,760,505],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5051"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5051"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5051\/revisions"}],"predecessor-version":[{"id":51924,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5051\/revisions\/51924"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}