Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a structure 1

Status
Not open for further replies.

jwdcfdeveloper

Programmer
Mar 20, 2001
170
0
0
US
I have created a cfc that passes back a structure. My problem is getting the structSort function to pass back all of the values here is the code that I have to create the sort

<!--- now create an array and sort the values --->
<cfset arrOrderedEvents = structSort(stcEvents,"numeric","asc","eventId")>

<!--- create new struct --->
<cfset thisEvent= structNew()>

<!--- loop over the array and order the structure --->
<cfloop from="1" to="#arrayLen(arrOrderedEvents)#" index="i">
<cfset thisEvent = stcEvents[arrOrderedEvents]>
</cfloop>

<cfreturn thisEvent>

However, the thisEvent structure only returns one row of data and should return 6 rows, any ideas about why this is occurring?

Thanks,

JW
 
<cfset thisEvent = stcEvents[arrOrderedEvents]>

you keep overwriting the same variable (thisEvent), your only going to get the last item in arrOrderedEvents


=========================================
I have not failed. I've just found 10,000 ways that won't work.
Thomas A. Edison
 
well i dont fully understand what's going on with

structSort(stcEvents,"numeric","asc","eventId")

is eventId a substructure in stcEvents? i'm assuming so.

Either way, if you are trying to build a structure, use the StructInsert function instead of setting a scalar variable (thisEvent in this case) even though you defined it as a struct, you are treating it like a constant.

maybe something like:
<cfloop from="1" to="#arrayLen(arrOrderedEvents)#" index="i">
<cfscript>
StructInsert(thisEvent,arrOrderedEvents],"value?");
</cfscript>
</cfloop>

the reason i put value? is because arrOrderedEvents is an array of keynames, you would have to go back to the original struct to get your values, but then again im not exactly sure of what the end result of this is supposed to be.


=========================================
I have not failed. I've just found 10,000 ways that won't work.
Thomas A. Edison
 
Actually what I am trying to do is sort the structure so that the it appears in the order of the day value. I can currently get the structure with all of the values. However, I need a way to sort the values by day. The structre will appear something like this.

stcEvents.eventId = 1111
stcEvents.day = 30
stcEvents.month = 5
stcEvents.title= "Memorial Day"


stcEvents.eventId = 2222
stcEvents.day = 3
stcEvents.month = 5
stcEvents.title= "Teacher's Day"


stcEvents.eventId = 3333
stcEvents.day = 8
stcEvents.month = 5
stcEvents.title= "Mother's Day"


What I need to do is sort these values so that they appear like this:


stcEvents.eventId = 2222
stcEvents.day = 3
stcEvents.month = 5
stcEvents.title= "Teacher's Day"

stcEvents.eventId = 3333
stcEvents.day = 8
stcEvents.month = 5
stcEvents.title= "Mother's Day"

stcEvents.eventId = 1111
stcEvents.day = 30
stcEvents.month = 5
stcEvents.title= "Memorial Day"

Where the day field dictates the order. The problem is that for some values the day field may be 0, and I have to go to another cfc to populate the needed values, which it does, but I can't get the order correct. Is there a way to sort the values and encapsulate this new sorted order in a structure?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top