Converts a struct to an ordered one
structToSorted(structure, callback)
structToSorted(structure, sorttype, sortorder, localeSensitive)
→ returns struct
structure.toSorted(callback)
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
text
numeric
text
asc
asc
desc
false
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
Signup for cfbreak
to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.