Sorts array elements.
arraySort(array, sortType [, sortOrder [, localeSensitive ]])
or
arraySort(array, callback)
returns boolean
someArray.sort(sortType [, sortOrder, localeSensitive ])
Name | Type | Required | Default | Description |
---|---|---|---|---|
array | array | Yes | Name of an array | |
sortType | string | 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 |
|
sortOrder | string | No | 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 |
callback | function | No | CF10+ 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 | No | false | CF10+ Specify if you wish to do a locale sensitive sorting. |
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) );
CF11+ Lucee4.5+
someArray = ["COLDFUSION","coldfusion","adobe","LucEE","RAILO"];
someArray.sort("text","desc");
writeOutput( serializeJSON(someArray) );
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) );
Takes an array of structs and sorts by multiple different keys, similar to the way a query allows.
arrayOfStructs = [
{
"userId": 1,
"firstName": "John",
"lastName": "Doe",
"departmentName": "Sales",
"active": 1
},
{
"userId": 2,
"firstName": "Jane",
"lastName": "Smith",
"departmentName": "Marketing",
"active": 1
},
{
"userId": 3,
"firstName": "Alice",
"lastName": "Johnson",
"departmentName": "Sales",
"active": 0
},
{
"userId": 4,
"firstName": "Bob",
"lastName": "Brown",
"departmentName": "Sales",
"active": 1
},
{
"userId": 5,
"firstName": "Charlie",
"lastName": "Davis",
"departmentName": "Marketing",
"active": 0
}
];
arrayOfStructs.sort((user1,user2) => {
if (user1.active != user2.active) {
return user2.active - user1.active;
}
if (user1.departmentName == user2.departmentName || user1.active == 0) {
if (user1.lastName == user2.lastName) {
return user1.firstName.compare(user2.firstName);
}
return user1.lastName.compare(user2.lastName);
}
return user1.departmentName.compare(user2.departmentName);
});
writeDump(arrayOfStructs);