ListSort

Description

Sorts list elements according to a sort type and sort order.

Returns

A copy of a list, sorted.

Category

List functions

Function syntax

ListSort(list,sortType,[sortOrder=asc, delimiters=',' ,includeEmptyFields=false, localeSensitive=false])

Syntax with callback

ListSort(list,callback)

History

ColdFusion (2021 release): Made the following changes:

  • Sort Member functions accept compare/compareNocase as callback.

ColdFusion (2018 release): Introduced named parameters.

ColdFusion 10: Added support for all Java supported locale-specific characters (including support for umlaut characters). A flag for this support has been added for sorttype = "text" or sorttype = " textnocase ".

ColdFusion MX: Changed the order in which sorted elements are returned: in a  textnocase , descending sort, this function might return elements in a different sort order than in earlier releases. If  sortTtype  = " textnocase " and sortOrder = "desc", ColdFusion MX processes elements that differ only in case differently from earlier releases. ColdFusion MX outputs the elements in the reverse of the ascending order. Earlier releases do not change order of elements that differ only in case. Both operations are correct. The new operation ensures that an ascending and descending sort output elements in exactly reverse order .For example, in a  textnocase , desc sort of d ,a ,a ,b ,A , the following occurs:

  • ColdFusion MX returns d,b,A,a,a
  • Earlier ColdFusion releases return d,b,a,a,A(In a  textnocase ,  asc  sort, all ColdFusion releases return a,a,A,b,d.)

Parameters

Parameter

Required/Optional

Default

Description

list

Required

 

A list or a variable that contains one.

includeEmptyFields

Optional

no

Set to yes to include empty fields.

localeSensitive

Optional

false

Specify if you wish to do a locale-sensitive sorting. The default value is false.

sortType

Optional

 

  • numeric: sorts numbers
  • text: sorts text alphabetically, taking  case  into account (also known as case sensitive). All letters of one case precede the first letter of the other case:
  • aabzABZ , if sortOrder = " asc " (ascending sort)- ZBAzbaa, if sortOrder = "desc" (descending sort)
  • textnocase : sorts text alphabetically, without regard to case (also known as case-insensitive). A letter in varying cases precedes the next letter:
  • aAaBbBzzZ, in an ascending sort; preserves original intra-letter order - ZzzBbBaAa, in a descending sort; reverses original intra-letter order

sortOrder

Required

asc

  • asc  - ascending sort order. Default.
  • aabzABZ  or aAaBbBzzZ, depending on  value  of sortType, for letters- from smaller to larger, for numbers
  • desc - descending sort order.
  • ZBAzbaa or ZzzBbBaAa, depending on  value  of sortType, for letters- from larger to smaller, for numbers

delimiters

Optional

,

A string or a variable that contains one. Characters that separate 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,  and ignores the rest.

callback

Optional

 

A function that takes two elements of the list and returns 1, 0 or -1, if the first value is greater than, equal to, or less than the second value, respectively.

Usage

ColdFusion ignores empty list elements; thus, the list "a,b,c,,,d" has four elements.

Example

<cfscript>
    // case 1
    myList1="12,23,107,19,1,65"
    sortedNums=ListSort(myList1,"Numeric","asc",",")
    writeOutput(sortedNums & "<br/>")
    // case 2
    myList2="23.75;-34,471:100,-9745"
    sortedNums2=ListSort(myList2,"Numeric","asc",";:,")
    writeOutput(sortedNums2 & "<br/>")
    // case 3
    myList3="hello;123,HELLO:jeans,-345,887;ColdFusion:coldfusion"
    sortedMix=ListSort(myList3,"TextNoCase","asc",";,:")
    writeOutput(sortedMix)
</cfscript>

Output

1,12,19,23,65,107
-9745;-34;23.75;100;471
-345;123;887;ColdFusion;coldfusion;hello;HELLO;jeans

Example with callback

<cfscript>
       myList="ColdFusion,COLDFUSION,Cold Fusion,Coldfusion";
       // define callback function
       function callback(e1,e2){
             return compareNoCase(e1,e2);
       }
       mySortedList=ListSort(myList,callback);
       writeOutput(mySortedList);
</cfscript>

Example using compareNoCase as callback.

<cfscript> 
    listToSort = "d,C,b,A" 
    sortedList = listToSort.listSort(compareNoCase) 
    writeDump(sortedList) 
</cfscript>

Output

A,b,C,d

Example using compare as callback.

<cfscript> 
    listToSort = "d,C,b,A" 
    sortedList = listToSort.listSort(compare) 
    writeDump(sortedList) 
</cfscript>

Output

A,C,b,d

 Adobe

Schneller und einfacher Hilfe erhalten

Neuer Benutzer?