Different ways of sorting using GORM in Grails
Posted By : Nishtha Singh | 29-Jul-2013
Sorting is one of the important aspects when presenting data to the end user . Sort data by date , by name etc . In this blog I will describe sorting in different ways in Grails
-
Sort query
Customizes the default property to sort by for query results. We can apply this property in the domain class as shown below:
package myapp1; class StudentDB { String name; String rollNo; String age; String marks; String stream; String contact; static constraints = { } static mapping = { sort name: "asc"} }
This property will automatically sort the list in the ascending order. In the controller I have just fetched the list of student details in ascending order as shown below.def student=StudentDB.list(sort:"name") log.debug "student= "+student render (template:"showResult", model:[student:student])
Hence the output is as under:-
Name: nishtha RollNo: 101 Age: 21 Marks: 89 Stream: sci Contact: 778890098
Name: richa RollNo: 103 Age: 22 Marks: 83 Stream: sci Contact: 76876989080
Name: suraj RollNo: 102 Age: 22 Marks: 87 Stream: sci Contact: 7788998098 -
Sort () method:
This method is simply used to sort the list according to a specified property. We dont need to specify anything in the domain class as in the previous case. I have used the same example where I have fetched the list of student details from the database and used the method sort() to sort the list according to the property name. The below code for controller’s action illustrates that.:-
def student=StudentDB.findAll() def results=student.sort{it.name} log.debug "student= "+student render (template:"showResult", model:[results:results])
The output is same as shown above and the template has been rendered to display the output by iterating the list.
-
Sort using closure
This type of sorting is implemented using closure where we need to define two dynamic variables. The method of String class compareToIgnoreCase() compares the two elements of the same list on the basis of specified property and returns the sorted list.In my example, the elements of the list ‘results’ are objects of StudentDb domain class.So here is the controller’s code.
def showResult = { def results=StudentDB.findAll() results.sort{ stu1, stu2 -> stu1.name.compareToIgnoreCase(stu2.name) } render (template:"showResult", model:[results:results]) }
The gsp code and output are same as above
We can also use the above sort closure to obtain the sorted list coming from another domain which has been mapped with the current domain.For instance suppose I have a domain:
class StudentDB { String name; String rollNo; String age; String marks; String stream; String contact; static belongsTo = [studentLastName:StudentLastName] static constraints = { } static mapping = { }
And the other domain is as follows which contains the last names of the students:
class StudentLastName { String lastName; static constraints = { }
The controller’s action code is as follows:-
def showResult = { def results=StudentDB.findAll() results.sort{ stu1, stu2 -> stu1.studentLastName.lastName.compareToIgnoreCase(stu2.name) } render (template:"showResult", model:[results:results]) }
-
Sorting of list using key value of Objects:
We can use the sort method with closure to sort the list of Json objects as well. A Json object is an unordered collection of key/value pairs.
The values can be any of these types:Boolean, JSONArray, JSONObject, Number, String, or the JSONObject. So in my example I have sorted a list of json objects on the basis of a specific property i.e lastName. The controller’s action code is shown below:
def showResult = { def lastNameList=[] JsonObject json1=new JSONObject().put("name", "Varun") json1.put("lastName","Mehra") JsonObject json2=new Jsonobject().put("name","Smriti") json2.put("lastName","Sharma") JsonObject json3=new Jsonobject().put("name","Pulkit") json1.put("lastName","Gupta") lastNameList.add(json1); lastNameList.add(json2); lastNameList.add(json3); lastNameList.sort{ obj1,obj2 -> obj1.lastName.compareToIgnoreCase(obj2.lastName) } render (template:"showResult", model:[lastNameList:lastNameList]) }
Here we have sorted the list according to the last names. And in the template I have just iterated this list to show the output. The gsp code is as follows:
And the output is as under:
Gupta
Mehra
Sharma
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Nishtha Singh
Nistha is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Nistha's hobbies are poetry and glass painting.