July 17, 2009
ColdFusion Tip: Query columns in correct order
One quirk in Coldfusion that I've had to battle before is that columns in query objects are sorted in alphabetical order. Normally this isn't too much of an issue because you just output the columns in the desired order when you're displaying the output on a page. But there are times when you need to loop over a list of the columns and you'd like them to appear in the order defined in the SQL query or DB schema.
You can use queryObj.columnList to get a list of query's columns, but as I already mentioned this list is sorted alphabetically. To get a column list in the correct order, use this simple trick:
ArrayToList( queryObj.getColumnNames() )
This method appears to be an undocumented method of the underlying Java query object.