StructToSorted
Converts a struct to a sorted struct - using either a callback comparator or textual directives as the sort option
Method Signature
StructToSorted(struct=[structloose], sortType=[any], sortOrder=[string], localeSensitive=[any], callback=[function:Comparator])
Arguments
struct
struct
true
The struct to sort
sortType
any
false
An optional sort type to apply to that type - if a callback is given in this position it will be used as that argument
text
sortOrder
string
false
The sort order applicable to the sortType argument
asc
localeSensitive
any
false
false
callback
function:Comparator
false
An optional callback to use as the sorting function. You can alternatively pass a Java Comparator.
Examples
structToSorted with an inline function sorting by key value
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( Any value1, Any value2, Any key1, Any key2 ) {
if( value1 > value2 ) {
return -1;
}
return 1;
}
sortedStruct = StructToSorted( someStruct, sortByNumericValuesDescending );
writedump( sortedStruct );
Result: A new struct with the keys ordered by value descending
structToSorted member function with an inline function sorting by key value
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( Any value1, Any value2, Any key1, Any key2 ) {
if( value1 > value2 ) {
return -1;
}
return 1;
}
sortedStruct = someStruct.ToSorted( sortByNumericValuesDescending );
writedump( sortedStruct );
Result: A new struct with the keys ordered by value descending
structToSorted member function with an inline function sorting 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
};
function sortByKeyName( Any value1, Any value2, Any key1, Any key2 ) {
return compareNoCase( key1, key2 );
}
sortedStruct = someStruct.ToSorted( sortByKeyName );
writedump( sortedStruct );
Result: A new struct with the keys ordered by key name
structToSorted member function with sorttype argument
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 );
Result: A new struct with the keys ordered by key name
Additional Examples
myNestedStruct = {
"apple" : {
"price" : 3,
"quantity" : 10
},
"banana" : {
"price" : 1,
"quantity" : 5
},
"cherry" : {
"price" : 2,
"quantity" : 8
},
"date" : {
"price" : 4,
"quantity" : 12
}
};
writeDump( var=myNestedStruct, label="Before sorting" );
writeDUmp( var=StructToSorted( myNestedStruct, "textNoCase", "desc", true ), label="After sorting" );
myNumb.4 = "5";
myNumb.3 = "2";
myNumb.2 = "3";
myNumb.1 = "1";
cb = ( Any value1, Any value2, Any key1, Any key2 ) => {
if( arguments.VALUE1 < arguments.VALUE2 // i.e. desc
) return 1;
else return -1;
};
writeDump( var=myNumb, label="Before sorting" );
writeDUmp( var=StructToSorted( myNumb, cb ), label="After sorting" );
Related
Last updated
Was this helpful?