Dynamic Sorting with CFSWITCH

Dynamic Sorting

I find that I frequently need to sort and view records in ascending and descending order for several fields. This was at first very complex and cumbersome, but with the help of cfswitch you can sort as many fields as you want in any order you want. I'll explain below.

Lets say you are Pablo Varando and have 489 DVD's in your collection. Lets also say you actually wanted to list all the specs about the movies so that friends and family can find particular movies quickly.

For the sake of time I'll use my movie collection as an example. I only own a few movies so it makes it easier to create a sample database.

In the example below I have only 6 movies. As pathetic as my movie collection is, the ability to sort any given field would be very useful if I had Pablo's movie collection.

If you click any linked header title in the example below it will change the dynamic sorting order on the fly. As you can see this can be quite useful if you wanted to see all the movies made in 1999 or by Warner Bros.

How it Works

Lets start with the query. This is a generic query with an ORDERED BY statement in it. Typically you would just specify what field you wanted to order and whether it was in ascending or descending order.

Since this is a dynamic sort we need to create a cfqueryparam and set it 1. In this case it will order the query first by MovieTitle, then ProductionCompany.

Each possible ordering value is set in a cfcase with the values being indicate therein. As you can see, the values are grouped in pairs, (1 & 2 order the same fields just in different orders.) That way if you click the header link once it will order  in descending order. if you click it again it will be the opposite of that (ascending) In the example below I ordered each field so I can toggle it in the page.

<cfparam name="sort" default="1">

<cfquery name="qryMovies" datasource="MovieList">
      SELECT *
      FROM MovieList
      ORDER BY
<cfswitch expression="#sort#">

<cfcase value="1">MovieTitle, ProductionCompany</cfcase>
<cfcase value="2">
MovieTitle desc, ProductionCompany</cfcase>
<cfcase value="3">
ProductionCompany</cfcase>
<cfcase value="4">
ProductionCompany desc</cfcase>
<cfcase value="5">
MovieYear, MovieTitle, ProductionCompany</cfcase>
<cfcase value="6">
MovieYear desc, MovieTitle, ProductionCompany</cfcase>
<cfcase value="7">
Catagory, MovieTitle, ProductionCompany</cfcase>
<cfcase value="8">
Catagory desc, MovieTitle, ProductionCompany</cfcase>

</cfswitch>
</cfquery>

Next comes the html output. Nothing too fancy here. About the only thing we need to look at is the header row. This row contains the links to change the sort order for the given fields. The main thing here is the IIF statement. It checks to see what the value is currently set to, and makes the link for the opposite sort order. For example, lets say the default sort was used to generate the page. (1) Since you would want the next click of the link to change the sort order, the link has to change to a sort value of 2. That's all there is to it. The values are checked for each header, and changed accordingly.

<table border="1" cellpadding="4" cellspacing="0" width="100%">

<tr>
<td colspan="4"><!--- Title Row --->
<h3>
Movie Inventory</h3>
</td>
</tr>

<cfoutput>
<tr bgcolor="##E5E5E5"><!--- Header Row --->
<td><a href="movies.cfm?sort=#IIF(sort is 1, '2', '1')#">
Movie Title</a></td>
<td><a href="movies.cfm?sort=#IIF(sort is 3, '4', '3')#">
Production Company</a></td>
<td><a href="movies.cfm?sort=#IIF(sort is 5, '6', '5')#">
Movie Year</a></td>
<td><a href="movies.cfm?sort=#IIF(sort is 7, '8', '7')#">
Category</a></td>
</tr>
</cfoutput>

<cfoutput query="qryMovies"><!--- Data Rows --->
<tr>
<td>
#qryMovies.MovieTitle#</td>
<td>
#qryMovies.ProductionCompany#</td>
<td>
#qryMovies.MovieYear#</td>
<td>
#qryMovies.Catagory#</td>
</tr>
</cfoutput>

</table>

That's it you done! To view a working example go to http://www.pixeljunkie.org/easycfm/index.cfm?inc=mov

Download all associated files of this tutorial!



All ColdFusion Tutorials By Author: Mark Aplet
  • Adding an indexed Search to your site (Part 1)
    It is very easy to set up and create a professional search function much like a real search engine. Use Verity Collections and the tag to create fast search forms for your web sites.
    Author: Mark Aplet
    Views: 37,947
    Posted Date: Saturday, February 1, 2003
  • Adding an indexed Search to your site (Part 2)
    The long overdue part two of adding a verity search function. This part demonstrates how to index the information in your database so that it becomes usefull too.
    Author: Mark Aplet
    Views: 24,583
    Posted Date: Monday, May 5, 2003
  • Banning the spam
    Internet spam is on the rise, and more importantly spammers are targeting your sites comment forms. They are looking for the trackback urls to fool search engines into ranking their website higher in the search results. When this started to happen to me, I wanted to sent out emails to the offenders demanding that they stop. Unfortunatly the spam is being generated by bots and programs not some pimple faced kid behind a keyboard. Banning IP addresses is not enough and rarely works since intelligent spammers hide their true identity anyway. Next approach... Banning Keywords used by the offending sites. Thats where this tutorial comes in.
    Author: Mark Aplet
    Views: 11,144
    Posted Date: Wednesday, March 15, 2006
  • Changing site color scheme
    Add some personalization to your pages by letting the user pick their own color scheme. It's really quite simple and the benifits are awsome. This tutorial shows you how to set up your pages to use a dynamicly included scheme. It will also show you how to create all the pages neccessary to administer the color schemes.
    Author: Mark Aplet
    Views: 21,811
    Posted Date: Friday, November 29, 2002
  • Color Picker
    Sometimes, you want to be able to change the color of something on your page. Be it one item, or every item on the page. Using this simple color picker, you can create admin areas that can allow you or your visitors to pick their own colors and the value is automatically inserted into a text field.
    Author: Mark Aplet
    Views: 15,567
    Posted Date: Saturday, July 12, 2003
  • Dynamic Sorting with CFSWITCH
    Quickly and easily sort and order records in your database using a cfswitch in your query. Great technique for admin areas of your site, or just allowing visitors to sort the fields they want.
    Author: Mark Aplet
    Views: 17,913
    Posted Date: Sunday, August 3, 2003
  • Improving Application Performance
    One thing I am always trying to do is speed up my applications. As my site grows in size and complexity I find that I spend a fair amount of time re-coding pages because of a new technique I just learned. I wish I had learned about these techniques long before, and thereby allowing me to create more effective code. In this tutorial I'll try to explain some problem areas that I have identified, and some of the things you can do to improve performance.
    Author: Mark Aplet
    Views: 23,634
    Posted Date: Monday, January 12, 2004
  • Improving Application Performance (Part 2)
    Not all queries can be saved as an application variable. For Queries that do not meet the checkpoints in my previous tutorial there is another way to improve performance. Query Caching is another way to save data and eliminate unnecessary queries. This is for queries that are more dynamic in nature.
    Author: Mark Aplet
    Views: 12,436
    Posted Date: Monday, January 12, 2004
  • Slighty better search
    Someone on the forum posed a question a short while ago asking how to create a more advanced search function using a + symbol as a separator. So I created this advanced search function. This search function is just slightly better than a normal search as it adds the ability to separate two keywords with a + symbol. Lets start with the search form.
    Author: Mark Aplet
    Views: 24,066
    Posted Date: Thursday, December 4, 2003