|
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Sort #FORM# by value?
Any ideas? You would think that StructSort would be the answer but nope.Morgan Senkal 05/19/04 03:41 P Hi Morgan:Charlie Griefer 05/20/04 12:40 A when you say you need to retain the original key name...how so? a 2d array?Charlie Griefer 05/20/04 06:19 P Ok, for starters I tried adding a simple if statement like so:Morgan Senkal 05/20/04 06:01 P if you want it to insert only numeric values...the UDF should look like:Charlie Griefer 05/20/04 06:27 P Ah, thanks for clearing that up. I was testing kArray[i] insteadMorgan Senkal 05/20/04 06:52 P Morgan:Charlie Griefer 05/20/04 07:02 P It's working perfect Charlie, thanks. One small change I made on the second line of the function:Morgan Senkal 05/20/04 07:50 P Any ideas? You would think that StructSort would be the answer but nope. TIA! -Morgan Hi Morgan: Might be a better/more efficient way, but threw together a quick UDF that seems to work: <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <cfscript> function sortForm(stForm) { var formValueArray = arrayNew(1); var kArray = structKeyArray(stForm); for (i=1; i LTE arrayLen(kArray); i=i+1) { formValueArray[arrayLen(formValueArray)+1] = stForm[kArray[i]]; } temp = arraySort(formValueArray, 'text'); return formValueArray; } </cfscript> </head> <body> <cfparam name="form.field1" default="foo"> <cfparam name="form.field2" default="bar"> <cfparam name="form.field3" default="foobar"> <cfparam name="form.field4" default="barfoo"> <cfdump var="#form#"> <cfset formValueArray = sortForm(form)> <cfdump var="#formValueArray#"> </body> </html> Hope that helps. Any ideas? You would think that StructSort would be the answer but nope. TIA! -Morgan I suppose I should have given more information, sorry :-( But to clarify: I only want to pull out FORM values which are numeric and order those. I also need to somehow retain the key name associated to each value. It's a good start tho thanks much Charlie! Perhaps you and I can fiddle with it and see where we get... ----- Excess quoted text cut - see Original Post for more ----- when you say you need to retain the original key name...how so? a 2d array? a structure? Charlie > I suppose I should have given more information, sorry :-( But to clarify: I only want to pull out FORM values which are numeric and order those. I also need to somehow retain the key name associated to each value. It's a good start tho thanks much Charlie! Perhaps you and I can fiddle with it and see where we get... ----- Excess quoted text cut - see Original Post for more ----- Ok, for starters I tried adding a simple if statement like so: if (isNumeric(kArray[1])) { formValueArray[arrayLen(formValueArray)+1] = stForm[kArray[i]]; } But apparently it doesn't recognize any of the values as numbers. This is the cfdump I'm getting back for the FORM element: ACTIONDATE1 ACTIONDATE2 ACTIONITEMDESCRIPTION ACTIVITYTYPE ACTIVITYTYPEORDER 5 BUSINESSUNIT BUSINESSUNITORDER 3 DATE1 5/13/2004 DATE2 5/20/2004 FIELD1 foo FIELD2 bar FIELD3 foobar FIELD4 barfoo FIELDNAMES ISSUEDESCRIPTIONORDER,ISSUEDESCRIPTION,ISSUEOUTCOME,ACTIONITEMDESCRIPTION,GENTEXT,ACTIVITYTYPEORDER,ACTIVITYTYPE,DATE1,DATE2,TRIBEORDER,TRIBE,BUSINESSUNITORDER,BUSINESSUNIT,PARTICIPANT,ISSUECATEGORY,PRIORITY,PROJECTORDER,PROJECT,ISSUESTATUSORDER,ISSUESTATUS,ACTIONDATE1,ACTIONDATE2,QUERYNUMBER,QUERYTITLE,QUERY0 GENTEXT ISSUECATEGORY ISSUEDESCRIPTION ISSUEDESCRIPTIONORDER 9 ISSUEOUTCOME ISSUESTATUS ISSUESTATUSORDER 10 PARTICIPANT PRIORITY PROJECT PROJECTORDER 4 QUERY0 Run Query QUERYNUMBER 0 QUERYTITLE Custom Query TRIBE TRIBEORDER 1 So there are indeed numbers in there, but the question of whether they are 'numeric' objects is apparently no. Not sure the best way to proceed at this point... ----- Excess quoted text cut - see Original Post for more ----- Doesn't matter, as long as I can access the name in relation to the value. On second thought, I think the way I coded the form, I don't believe that it is possible for there to be repeat values. (all the form values in question are from checkboxes, so I can control the values sent). So actually the code you started will work, as long as it only includes those values that are numeric (and not empty), so I can do a numeric sort. ----- Excess quoted text cut - see Original Post for more ----- if you want it to insert only numeric values...the UDF should look like: <cfscript> function sortForm(stForm) { var formValueArray = arrayNew(1); var kArray = structKeyArray(stForm); for (i=1; i LTE arrayLen(kArray); i=i+1) { if (isNumeric(stForm[kArray[i]])) { formValueArray[arrayLen(formValueArray)+1] = stForm[kArray[i]]; } } temp = arraySort(formValueArray, 'numeric'); return formValueArray; } </cfscript> this does NOT yet retain the form field name. still not sure how you intend to handle that. ----- Excess quoted text cut - see Original Post for more ----- the cfdump I'm getting back for the FORM element: Ah, thanks for clearing that up. I was testing kArray[i] instead of stForm[kArray[i]]. As for the keys, as I mentioned above there should be no chance of having duplicate numeric values returned, so I can just match up the values to their keys in the original form now that they are in order. ----- Excess quoted text cut - see Original Post for more ----- Morgan: This seems to do it: <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <cfscript> function sortForm(f) { var sortArray = arrayNew(2); var stForm = f; var keys = structKeyArray(stForm); var sortedKeys = ""; for (i=1; i LTE #arrayLen(keys)#; i=i+1) { if (NOT isNumeric(stForm[keys[i]])) { structDelete(stForm, keys[i]); } } sortedKeys = structSort(stForm, 'numeric'); for (i=1; i LTE #arrayLen(sortedKeys)#; i=i+1) { sortArray[arrayLen(sortArray)+1][1] = sortedKeys[i]; sortArray[arrayLen(sortArray)][2] = stForm[sortedKeys[i]]; } return sortArray; } </cfscript> </head> <body> <cfparam name="form.field1" default="foo"> <cfparam name="form.field2" default="bar"> <cfparam name="form.field3" default="12"> <cfparam name="form.field4" default="foobar"> <cfparam name="form.field5" default="4"> <cfparam name="form.field6" default="barfoo"> <cfparam name="form.field7" default="8"> <cfdump var="#form#" label="this is the original form"> <cfset formSorted = sortForm(form)> <cfdump var="#formSorted#" label="this should be sorted"> </body> </html> let me know if that works out. Charlie It's working perfect Charlie, thanks. One small change I made on the second line of the function: Changed: var stForm = f; Changed To: var stForm = Duplicate(f); I wanted to leave the original #form# intact for further processing. On testing the code I found that leaving out the Duplicate() function did not break the reference. ----- Excess quoted text cut - see Original Post for more -----
|
Mailing Lists
|
Latest Fusion Authority Articles
|
||||||