Created
September 3, 2010 17:58
-
-
Save mhayes/564266 to your computer and use it in GitHub Desktop.
ColdFusion array of structs sorting
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
component output="no" { | |
function init() { | |
variables.itemArray = []; | |
} | |
function append(item) { | |
ArrayAppend(variables.itemArray, arguments.item); | |
} | |
function sortBy(key){ | |
//by default we'll use an ascending sort | |
var sortOrder = "asc"; | |
//by default, we'll use a textnocase sort | |
var sortType = "textnocase"; | |
//by default, use ascii character 30 as the delim | |
var delim = "."; | |
//make an array to hold the sort stuff | |
var sortArray = []; | |
//make an array to return | |
var returnArray = []; | |
//grab the number of elements in the array (used in the loops) | |
var count = arrayLen(variables.itemArray); | |
//make a variable to use in the loop | |
var ii = 1; | |
//if there is a 2nd argument, set the sortOrder | |
if(arraylen(arguments) GT 1) | |
sortOrder = arguments[2]; | |
//if there is a 3rd argument, set the sortType | |
if(arraylen(arguments) GT 2) | |
sortType = arguments[3]; | |
//if there is a 5th argument, set the delim | |
if(arraylen(arguments) GT 3) | |
delim = arguments[4]; | |
//loop over the array of structs, building the sortArray | |
for(ii = 1; ii lte count; ii = ii + 1) | |
sortArray[ii] = variables.itemArray[ii][key] & delim & ii; | |
//now sort the array | |
arraySort(sortArray,sortType,sortOrder); | |
//now build the return array | |
for(ii = 1; ii lte count; ii = ii + 1) | |
returnArray[ii] = variables.itemArray[listLast(sortArray[ii],delim)]; | |
//return the array | |
variables.itemArray = returnArray; | |
return variables.itemArray; | |
} | |
function getIterator() { | |
return variables.itemArray.Iterator(); | |
} | |
//for debugging only | |
function getArray() {return variables.itemArray;} | |
} |
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
//TYPE=FIELD | |
//ENVIRONMENT=INTERNAL | |
"WICONST.WELL", "Well Code" | |
"WIDATA.CO, COUNTY.COUNTY_NAME", "County" | |
//TYPE=FIELD | |
//ENVIRONMENT=EXTERNAL | |
"WIDATA.CO, COUNTY.COUNTY_NM", "County" | |
"WIWELLKEY.WELL_KEY", "Well Key" | |
//TYPE=FIELD | |
//ENVIRONMENT=COMMON | |
"WIDATA.AGENCY, WIAGENCY.ANAME", "Agency" | |
"WIDATA.SDATE", "Sample Date" | |
"WIDATA.NCHEM, WICHEM.CHNAME", "Pesticide" | |
"WIDATA.CONC", "Amount Detected" | |
"WIDATA.MDL", "Detection Limit" | |
"WIDATA.LAB, WIANLAB.LABNAME", "Lab" | |
"WIDATA.ADATE", "Analysis Date" | |
"WIDATA.APFILE", "Study Name", | |
"WIDATA.RPT_YR", "Report Year" | |
"WIDATA.WATD", "Water Depth" | |
"WIDATA.SOURCE", "Contamination Source" | |
"WIDATA.STATUS", "Detection Status" | |
"WIDATA.METHOD_NOTE", "Lab Method Note" | |
"WICONST.DEPTH", "Well Depth" | |
"WICONST.CODE", "Well Type Code" | |
"WICONST.MTRS", "Well Location Code - MTRS" | |
//TYPE=FILTER | |
//ENVIRONMENT=COMMON | |
"WIDATA.SDATE", "GREATER THAN, LESS THAN, EQUAL TO, BETWEEN" | |
"WIDATA.AGENCY" => "[WIAGENCY.AGE, WIAGENCY.ANAME]" | |
"WICHEM.CHNAME", "LIKE" | |
"WIDATA.CONC", "GREATER THAN" | |
"WIDATA.RPT_YR", "GREATER THAN, LESS THAN, EQUAL TO, BETWEEN" | |
"WIDATA.CO" => "[COUNTY.COUNTY_CD, COUNTY.COUNTY_NM]" | |
"WICONST.CODE", "LIKE" | |
//TYPE=FILTER | |
//ENVIRONMENT=INTERNAL | |
"WIDATA.APFILE", "LIKE" |
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
<!--- SET TYPES ---> | |
<cfscript> | |
types.FIELD = "^//TYPE="; | |
types.ENVIRONMENT = "^//ENVIRONMENT="; | |
fields.SINGLE = '^"[A-Za-z]+\.[A-Za-z]+", "[A-Za-z0-9 ]+"$'; | |
fields.MULTIPLE = '^"((\w+\.(\w+))(, )?)+", "\w+"$'; | |
filters.YEAR = "^\d{4}$"; | |
filters.DATE = "^\d{4}-\d{2}-\d{2}$"; | |
filters.STRING = "^\w+$"; | |
fieldArray = new dprarray(); | |
filterArray = new dprarray(); | |
</cfscript> | |
<cfloop file="#ExpandPath('./fields.txt')#" index="line"> | |
<!--- if current type or environment set, then skip current line ---> | |
<cfset skipLine = false /> | |
<!--- determine if this line denotes type/environment info ---> | |
<cfif REFind(types.FIELD, line)> | |
<cfset current.type = Right(line,len(line)-len(types.FIELD)+1) /> | |
<cfset skipLine = true /> | |
<cfelseif REFind(types.ENVIRONMENT, line)> | |
<cfset current.environment = Right(line,len(line)-len(types.ENVIRONMENT)+1) /> | |
<cfset skipLine = true /> | |
</cfif> | |
<!--- go ahead and process this line ---> | |
<cfif not skipLine> | |
<!--- common info ---> | |
<cfset item = StructNew() /> | |
<cfset item.environment = current.environment /> | |
<cfif current.type EQ "FIELD"> | |
<cfoutput> | |
#line# <cfdump var="#REFind(fields.MULTIPLE,line, 1, true)#" /> | |
<cfset item.breakdown = REFind(fields.MULTIPLE,line, true) /> | |
<cfset item.multiple = REFind(fields.MULTIPLE,line) EQ 1 /> | |
<cfset item.line = line /> | |
<cfset fieldArray.append(item) /> | |
</cfoutput> | |
<cfelseif current.type EQ "FILTER"> | |
<cfset item.line = line /> | |
<cfset filterArray.append(item) /> | |
</cfif> | |
</cfif> | |
</cfloop> | |
<cfset fieldArray.sortBy('LINE') /> | |
<cfset filterArray.sortBy('ENVIRONMENT') /> | |
<cfdump var="#fieldArray.getArray()#" label="Fields" /> | |
<cfdump var="#filterArray.getArray()#" label="Filters" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment