Created
March 12, 2020 11:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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