Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 12, 2020 11:13
Show Gist options
  • Save bennadel/0713d95ef81ac86bbe6e4a2b9cc03646 to your computer and use it in GitHub Desktop.
Save bennadel/0713d95ef81ac86bbe6e4a2b9cc03646 to your computer and use it in GitHub Desktop.
Using Subtraction To Power The Sort Comparison Operator In Lucee CFML 5.2.9.31
<cfscript>
values = [
"asdlfkj",
"oweiru",
"aldkfjlakjflajsdfljalsfjlfl",
"xzmcn",
"lakdfjlakjfl",
"mlkjwler",
"adf",
"lkasdjflajdla",
"cvuoixcviou"
];
// Sort the collection of values based on the LENGTH of each value.
values.sort(
( a, b ) => {
var aLength = a.len();
var bLength = b.len();
if ( aLength < bLength ) {
return( -1 );
} else if ( aLength > bLength ) {
return( 1 );
} else {
return( 0 );
}
}
);
dump( values );
</cfscript>
<cfscript>
values = [
"asdlfkj",
"oweiru",
"aldkfjlakjflajsdfljalsfjlfl",
"xzmcn",
"lakdfjlakjfl",
"mlkjwler",
"adf",
"lkasdjflajdla",
"cvuoixcviou"
];
// Sort the collection of values based on the LENGTH of each value.
values.sort(
( a, b ) => {
return( a.len() - b.len() );
}
);
dump( values );
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment