ListSort

Description

Sorts and delimits the items in a list according to a sort type and sort order.

Category

List functions

Syntax

ListSort(list, sort_type [, sort_order] [, delimiter ]) 

Parameters

Parameter
Description
list
List to sort. List items must be separated by commas or another delimiter.
sort_type
The type of sort to execute, among the following sort types:
  • Numeric - sorts numbers
  • Text - sorts text alphabetically
  • Textnocase - sorts text alphabetically. The case is ignored
sort_order
The order to follow. You can specify any of the following:
  • Asc - (Default) Ascending sort order
  • Desc - Descending sort order
delimiter
The character(s) used to delimit elements in the list. Default is comma.

Usage

ColdFusion ignores empty list elements; thus, a list that is defined as "a,b,c,,,d" is treated as a four element list.

Example

<!--- This example shows how to use ListSort--->
<html>
<head>
<title>ListSort Example</title>
</head>

<body>
<H3>ListSort Example</H3>

<!--- Find a list of users who wrote messages --->
<cfquery name = "GetMessageUser" datasource = "cfsnippets">
SELECT     Username, Subject, Posted
FROM     Messages
</cfquery>

<cfset myList = ValueList(GetMessageUser.UserName)>
<P>Here is the unsorted list. </P>

<cfoutput>
#myList#
</cfoutput>
<P>Here is the list sorted alphabetically:</P>
<cfset sortedList = ListSort(myList, "Text")>

<cfoutput>
#sortedList#
</cfoutput>

<P>Here is a numeric list that is to be sorted in descending order.</P>
<cfset sortedNums = ListSort("12,23,107,19,1,65","Numeric", "Desc")>
<cfoutput>
#sortedNums#
</cfoutput>

<P>Here is a list that must be sorted numerically, since it 
contains negative and positive numbers, as well as decimal numbers. </P>

<cfset sortedNums2 = ListSort("23.75;-34,471:100,-9745","Numeric", "ASC", ";,:")>

<cfoutput>
#sortedNums2#
</cfoutput>

<P>Here is a list to be sorted alphabetically without consideration
of case.</P>

<cfset sortedMix =
  ListSort("hello;123,HELLO:jeans,-345,887;ColdFusion:coldfusion",
    "TextNoCase", "ASC", ";,:")>

<cfoutput>
#sortedMix#
</cfoutput>

</body>
</html>