Created
February 8, 2024 18:25
-
-
Save tonyjunkes/a9d6b38f0558db2a8e624ed9c008a103 to your computer and use it in GitHub Desktop.
Example sorting alphanumeric column names for a query object created out of order (for example: cfspreadsheet).
This file contains 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> | |
// Column name example: col_1, col_10, col_6, etc. | |
var qryColumnOrder = myQuery.columnList | |
.listToArray() | |
.sort((a, b) => { | |
// Extract the numeric part from the string | |
var numA = numberFormat(a.reReplace("\D", "", "all"), "0000"); | |
var numB = numberFormat(b.reReplace("\D", "", "all"), "0000"); | |
// Extract the text part from the string | |
var textA = a.reReplace("\d", "", "all"); | |
var textB = b.reReplace("\d", "", "all"); | |
// First compare the text part | |
if (textA < textB) return -1; | |
if (textA > textB) return 1; | |
// If text parts are the same, compare the numeric part | |
return numA - numB; | |
}); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment