arraySort

Sorts array elements numerically or alphanumerically.

arraySort(array, sort_type [, sort_order])
or
arraySort(array, callback) → returns boolean

Member Function Syntax

someArray.sort(sort_type [, sort_order])

Argument Reference for the arraySort function

array

Required: Yes
Name of an array

sort_type

Required: Yes
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

sort_order

Required: No
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

Required: No
The callback function which take two elements of an array, Returns whether the first is less than (-1), equal to (0) or greater than (1) the second one(like as compare function).

Examples sample code invoking the arraySort function


Simple example for 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]


Simple example with member function

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"]


Simple example with callback function

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"}]


Fork me on GitHub