arraySort

Sorts array elements.

arraySort(array, sortType [, sortOrder [, localeSensitive ]]) or arraySort(array, callback) → returns boolean

Member Function Syntax

someArray.sort(sortType [, sortOrder, localeSensitive ])

Argument Reference

array array
Required

Name of an array

sortType string
Required

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 sort_order = "asc" (ascending sort)
- ZBAzbaa, if sort_order = "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
Values:
  • numeric
  • text
  • textnocase

sortOrder string
Default: asc

asc: ascending sort order. Default.
- aabzABZ or aAaBbBzzZ, depending on value of sort_type,
for letters
- from smaller to larger, for numbers

desc: descending sort order.
- ZBAzbaa or ZzzBbBaAa, depending on value of sort_type,
for letters
- from larger to smaller, for numbers
Values:
  • asc
  • desc

callback function

CF 10+ A function that uses two elements of an array. 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.

Compatibility

ColdFusion:

CF2018+ Member function returns the sorted array.

Examples
Sample code invoking the arraySort function

Uses the arraySort() function to get the sorted array and which sorted by type numeric

someArray = [10,20,-99,46,50];
arraySort(someArray, "numeric", "desc");
writeOutput( serializeJSON(someArray) );

Expected Result: [50,46,20,10,-99]

CF 11+ Lucee 4.5+

someArray = ["COLDFUSION","coldfusion","adobe","LucEE","RAILO"];
someArray.sort("text","desc");
writeOutput( serializeJSON(someArray) );

Expected Result: ["coldfusion","adobe","RAILO","LucEE","COLDFUSION"]

Uses the callback function

someArray = [
    {name="testemployee", age="32"},
    {name="employeetest", age="36"}
];
arraySort(
    someArray,
    function (e1, e2){
        return compare(e1.name, e2.name);
    }
);
writeOutput( serializeJSON(someArray) );

Expected Result: [{"NAME":"employeetest","AGE":"36"},{"NAME":"testemployee","AGE":"32"}]

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub