structToSorted

Converts a struct to an ordered one

structToSorted(structure, callback) structToSorted(structure, sorttype, sortorder, localeSensitive) → returns struct

Member Function Syntax

structure.toSorted(callback)

This function requires Adobe ColdFusion 2016.0.3 and up. Not supported on Lucee, OpenBD, etc.

structToSorted Argument Reference

structure struct
Required

Structure or a variable that contains one.

callback function

Closure or function reference that will be called for each iteration. Should return -1, 0 or 1. The arguments passed to the callback are:

value1: Value of the key/value pair in the struct
value2: Value of the key/value pair to compare against
key1: Key name of the key/value pair in the struct
key2: Key name of the key/value pair to compare against
Callback parameters:

Callback parameters:

  • value1 * any : The value of the first key/value pair
  • value2 * any : The value of the second key/value pair
  • key1 * string : The key name of the first key/value pair
  • key2 * string : The key name of the second key/value pair

sorttype string
Default: text

Values:
  • numeric
  • text

sortorder string
Default: asc

* asc: ascending (a to z) sort order.
* desc: descending (z to a) sort order
Values:
  • asc
  • desc

localeSensitive boolean
Default: false

Respect locale-specific characters (including support for umlaut characters) while sorting (applies to type"text").

Examples sample code invoking the structToSorted function

Use a function to sort the keys by numeric value descending

someStruct = {
    "NINE": 9,
    "SIX": 6,
    "THREE": 3,
    "TWELVE": 12,
    "tres": 3,
    "seis": 6,
    "nueve": 9,
    "doce": 12
};
     
function sortByNumericValuesDescending(value1, value2, key1, key2) {
        if (value1 > value2) {
            return -1;
        }
        return 1;
};
    
sortedStruct=StructToSorted(someStruct, sortByNumericValuesDescending);
writedump(sortedStruct);

Expected Result: A new struct with the keys ordered by value descending

Use a function to sort the keys by numeric value descending

someStruct = {
    "NINE": 9,
    "SIX": 6,
    "THREE": 3,
    "TWELVE": 12,
    "tres": 3,
    "seis": 6,
    "nueve": 9,
    "doce": 12
};
     
function sortByNumericValuesDescending(value1, value2, key1, key2) {
        if (value1 > value2) {
            return -1;
        }
        return 1;
};

sortedStruct=someStruct.ToSorted(sortByNumericValuesDescending);
writedump(sortedStruct);

Expected Result: A new struct with the keys ordered by value descending

Use a function to sort the keys by name

someStruct = {
    "NINE": 9,
    "SIX": 6,
    "THREE": 3,
    "TWELVE": 12,
    "tres": 3,
    "seis": 6,
    "nueve": 9,
    "doce": 12
};
     
function sortByKeyName(value1, value2, key1, key2) {
        return compareNoCase(key1, key2);
}

sortedStruct=someStruct.ToSorted(sortByKeyName);
writedump(sortedStruct);

Expected Result: A new struct with the keys ordered by key name

Use a function to sort the keys by name

someStruct = {
    "NINE": 9,
    "SIX": 6,
    "THREE": 3,
    "TWELVE": 12,
    "tres": 3,
    "seis": 6,
    "nueve": 9,
    "doce": 12
};
     
sortedStruct=someStruct.ToSorted("text","asc",false);
writedump(sortedStruct);

Expected Result: A new struct with the keys ordered by key name


Fork me on GitHub