List of Topics

SfC Home > Web Design > SQL > ColdFusion >


Explanation of Sorting a Table Listing in ColdFusion. Key words: database, SQL, query, ascend, descend, order, variable, <CFIF>, <CFELSE>, tags, IsDefined, URL, Adobe, School for Champions. Copyright © Restrictions

Sorting a Table Listing in ColdFusion

by Ron Kurtus (5 January 2003)

When you populate a table with items from a database query, using Macromedia ColdFusion, you can list items in ascending or descending order with the ORDER BY command in SQL. But you may also want to allow the user to resort a specific column in the table.

Questions you may have include:

This lesson will answer those questions.

Example of resorting


Name(sort descending)






Buffer solution


British thermal unit



The table above has been populated from a database query. If you click on the Number item in the heading, you can resort the listings. Clicking on Word will resort in a descending order.


The code for sorting the table list is relatively simple. Clicking on a heading item sends a variable to a set of <CFIF>... <CFELSE>... </CFIF> CFML tags on the same page, where the initial query and the sorting query exist. Then the table is populated from <CFOUTPUT>.


The <CFIF> tag shows its contents if the url.sorter variable has been sent.

<CFIF IsDefined("url.sorter")>

Then the query will be ordered by the value of url.sorter that is sent.

SELECT numb, name FROM Table_Name
ORDER BY #url.sorter#

If the variable has not been sent (like in the initial view of the page), we see the other query.


SELECT numb, name FROM Table_Name



The variable sorter is sent by clicking on a link. The result of sending the first variable will result in an ascending sort. The second variable sorter=word DESC will provide a descending sort.

<TD><A HREF="this_page.cfm?sorter=numb">Number</A> </TD>
<TD><A HREF="this_page.cfm?sorter=word DESC">Name</A></TD>


You can populate a table with items from a database query, and then sort according selected items in the heading using simple ColdFusion code.

See the Side Menu for more ColdFusion lessons

Keep code simple

Resources and references

Ron Kurtus' Credentials


ColdFusion Resources


Top-rated books on ColdFusion

Questions and comments

Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. I will try to get back to you as soon as possible.


Click on a button to bookmark or share this page through Twitter, Facebook, email, or other services:


Students and researchers

The Web address of this page is:

Please include it as a link on your website or as a reference in your report, document, or thesis.

Where are you now?

School for Champions

Web Design

ColdFusion topics

Sorting a Table Listing in ColdFusion

ColdFusion topics


Development methods

Specific applications

Also see

Let's make the world a better place

Be the best that you can be.

Use your knowledge and skills to help others succeed.

Don't be wasteful; protect our environment.

You CAN influence the world.

Live Your Life as a Champion:

Take care of your health

Seek knowledge and gain skills

Do excellent work

Be valuable to others

Have utmost character

Be a Champion!

The School for Champions helps you become the type of person who can be called a Champion.