Tuesday, October 26, 2010

Re-sort an existing Query without QoQ in ColdFusion

As I was looking today for a way to resort an existing query in my application, I though of the following options I had.

  1. Do a new call to SQL
  2. Create a QoQ and resort

Even though both options would work, I was unhappy with them. #1 because it is an additional call to the DB and #2 because I did not want to break out of my cfscript block. So I did a little googling and came up with the following post:

http://stackoverflow.com/questions/1166680/how-do-i-sort-a-query-object-in-coldfusion-7

Check out the 2nd post. Basically you can apply the underlying Java sort function to your query Object and it will sort it for you, Like so;

As per the original author of this hint:

The findColumn() call is needed to get the index of the sort column, because sort() is working with the column indexes and not with the column names. The second parameter specifies the sort order: TRUE = ascending, FALSE = descending.

  • Advantages: one-line call, faster than QoQ
  • Disadvantage: sort restricted to one column

There are much more hidden features of the underlying Java class. See the following links for more information:

Update 10.27.2010

I just wanted to add for all my ColdBox Friends or Newbies, remember we have a handy plugin available for us that does this and a bit more...

http://wiki.coldbox.org/wiki/Plugins:QueryHelper.cfm

No comments:

Post a Comment