- Introduction
- Creating an Array
- Adding an Element to an Array
- Displaying a Value in an Array
- Looping over an Array
- Manipulating Array Values
- Sorting an Array
- Multidimensional Arrays
- Creating Arrays with More Than Three Dimensions
- Array Aggregate Functions
- Array Utility Functions
- Array Information Functions
3.6. Sorting an Array
You want to sort the elements of an array.
Technique
Use the ArraySort() function to sort the elements of an array.
<cfset abcList = "c,B,b,A,C,a"> <cfset aAbcArray = ArrayNew(1)> <cfloop from="1" to="#ListLen(abcList)#" index="i"> <cfset aAbcArray[i] = listGetAt(abcList,i)> </cfloop> aAbcArray:<br/> <cfoutput> <cfloop from="1" to="#ArrayLen(aAbcArray)#" index="i"> element #i#: # aAbcArray[i]#<br/> </cfloop> </cfoutput> <p/> aAbcArray sorted with "text" attribute:<br/> <cfset ArraySort(aAbcArray,"text")> <cfoutput> <cfloop from="1" to="#ArrayLen(aAbcArray)#" index="i"> element #i#: # aAbcArray[i]#<br/> </cfloop> </cfoutput> <p/> aAbcArray sorted with "text" and "desc" attributes:<br/> <cfset ArraySort(aAbcArray,"text","desc")> <cfoutput> <cfloop from="1" to="#ArrayLen(aAbcArray)#" index="i"> element #i#: # aAbcArray[i]#<br/> </cfloop> </cfoutput> <p/> aAbcArray sorted with "textnocase" attribute:<br/> <cfset ArraySort(aAbcArray,"textnocase")> <cfoutput> <cfloop from="1" to="#ArrayLen(aAbcArray)#" index="i"> element #i#: # aAbcArray[i]#<br/> </cfloop> </cfoutput> <p/> aAbcArray sorted with "textnocase" and "desc" attributes:<br/> <cfset ArraySort(aAbcArray,"textnocase","desc")> <cfoutput> <cfloop from="1" to="#ArrayLen(aAbcArray)#" index="i"> element #i#: # aAbcArray[i]#<br/> </cfloop> </cfoutput>
Comments
The ArraySort() function sorts arrays numerically or alphabetically. It accepts two required arguments, the array to be sorted and the sort type, and one optional argument, sort order (which, if not provided, defaults to ascending).
Using ArraySort(), the sort type argument can be numeric, text, or textnocase. If sort type is numeric, the array elements are sorted numerically in ascending or descending order based on the sort order argument. If no sort order is provided, ArraySort() uses ascending, the default.
If sort type is text, the array elements are sorted alphabetically taking case into account. In text sorts, sort order ascending starts with uppercase A and ends with lowercase z, descending starts with lowercase z and ends with uppercase A. If sort type is textnocase, the array elements are sorted alphabetically without taking case into account. That is, if sort order is ascending, A/a precedes a B/b. If sort order is descending, a Z/z precedes a Y/y. Take a look at what happens when you execute the code in this technique:
aAbcArray: element 1: c element 2: B element 3: b element 4: A element 5: C element 6: a aAbcArray sorted with "text" attribute: element 1: A element 2: B element 3: C element 4: a element 5: b element 6: c aAbcArray sorted with "text" and "desc" attributes: element 1: c element 2: b element 3: a element 4: C element 5: B element 6: A aAbcArray sorted with "textnocase" attribute: element 1: a element 2: A element 3: b element 4: B element 5: c element 6: C aAbcArray sorted with "textnocase" and "desc" attributes: element 1: C element 2: c element 3: B element 4: b element 5: A element 6: a