listSort

Sorts list elements according to a sort type and sort order. Returns a sorted copy of the list.
[sortType - description]
numeric: sorts numbers
text: sorts text alphabetically, taking case into account
- aabzABZ, if sort_order = "asc"
- ZBAzbaa, if sort_order = "desc"
textnocase: sorts text alphabetically, without regard to case
- aAaBbBzzZ, in an asc sort;
- ZzzBbBaAa, in a desc sort;

listSort(list, sortType [, sortOrder] [, delimiters] [, includeEmptyFields] [, localeSensitive]) or listSort(list, callback) → returns string

Member Function Syntax

list.listSort(sortType [, sortOrder])

listSort Argument Reference

list string
Required

A list or variable name

sortType string
Required

numeric: sorts numbers
text: sorts text alphabetically, taking case into account
(also known as case-sensitive).
- aabzABZ for ascending sort(sort_order = "asc")
- ZBAzbaa for descending sort(sort_order = "desc")

textnocase: sorts text alphabetically, without regard to
case (also known as case-insensitive).
- aAaBbBzzZ for ascending sort(sort_order = "asc")
- ZzzBbBaAa for descending sort(sort_order = "desc")
Values:
  • numeric
  • text
  • aabzABZ
  • ZBAzbaa
  • textnocase
  • aAaBbBzzZ
  • ZzzBbBaAa

sortOrder string
Default: asc

- asc: ascending sort order
- desc: descending sort order
Values:
  • asc
  • desc

delimiters string
Default: ,

Characters that separate the list elements. The default value is comma. If this parameter contains more than one character, ColdFusion uses the first character in the string as the delimiter in the output list.

includeEmptyFields boolean
Default: false

Set to true to include empty fields.

callback function

CF 2016+ A function that uses two elements of a list. function(element1, element2). Returns whether the first is less than (-1), equal to (0) or greater than (1) the second one (like the compare functions).

localeSensitive boolean
Default: false

CF 10+ Specify if you wish to do a locale sensitive sorting.

Examples
Sample code invoking the listSort function

Uses the listSort() function to get the list which sorted by type text(case-sensitive)

list = "COLDFUSION,coldfusion,adobe,LucEE,RAILO";
sortList = listSort(list, "Text", "desc");
writeOutput(sortList);

Expected Result: coldfusion,adobe,RAILO,LucEE,COLDFUSION

Uses the listSort() function with delimiters to get the list which sorted by type numeric

list = "10;20;-99;46;50";
sortList = listSort(list, "Numeric", "asc", ";");
writeOutput(sortList);

Expected Result: -99;10;20;46;50

Uses the listSort() function with delimiters to get the list which sorted by type textnocase(case-insensitive)

list = "10|RED|yeLLow|-246|green|ORange";
sortList = listSort(list, "TextNoCase", "asc", "|");
writeOutput(sortList);

Expected Result: -246|10|green|ORange|RED|yeLLow


Fork me on GitHub