1/14/2011

Adding Order to a DynamicQuery in Liferay

I am always reacting to the fact when people visit my blog with google queries ... so someone was looking for "Adding order to Dynamic Queries". Well ... here is what I know about that:


If you already know a lot about the DynamicQuery in Liferay, just continue reading. If not, please start here:


http://liferay-blogging.blogspot.com/2010/11/dynamicquery-api-in-liferay.html


Adding Order To a DynamicQuery


The first thing to start is to take a look at the "OrderFactoryUtil" class which you can find in Liferays com.liferay.portal.kernel.dao.orm package. The first two methods you have to know something about are:


Order asc(String propertyName);
Order desc(String propertyName);


They simply tell the DynamicQuery you attach them, that the results should be ordered ascending or desceding according to the chosen property name. That´s all for those methods.


The last method offers you to implement your own comparator. You have to implement the OrderByComparator that works pretty much like any other Comparator you would implement in Java. You can find many example of that in the following package:


com.liferay.portlet.journal.util.comparator


rotty3000 added the following:


"The orderbycomparator is designed in such a way that it is translated into SQL instructions and amends the query."


So - go ahead and use the Comparator :)







If you liked this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.