Skip to main content
Known Participant
March 26, 2009
Question

Structure Soting Problem

  • March 26, 2009
  • 2 replies
  • 412 views
I have a structure called stcSpecialties. I'm trying to loop over the stucture and display it in ABC order. When I cfdump the structure it's in ABC order, but when I loop over it, it's not. I'm assuming CF must be auto sorting the stuct when doing a dump. How can I replicate this? I tried StructSort but I must be using it wrong. Here's a sample of the structure when I cfdump it:

Name = Value
------------------------------------------------
AmbulanceTransport = Ambulance Transport|3
BurnUnit = Burn Unit|4
CardiacCareUnit = Cardiac Care Unit|115
CardiacCathLab = Cardiac Cath Lab|21
CardiacIntensiveCareUnit = Cardiac Intensive Care Unit|7
CardiovascularIntensivecare = Cardiovascular Intensive care|48

but when I use the code below I get;
Medical Intensive Care Unit (49)
Gynechology (7)
Director (1)
Cardiac Intensive Care Unit (7)
Long Term Care (3)
IV Therapy (1)
Geriatrics (15)
Stepdown (74)

FYI, the value of the structure keys is a | delimeted list so I can display the actual name of the specialty which is the first list item and then the count which is the second list item
    This topic has been closed for replies.

    2 replies

    Inspiring
    March 26, 2009
    > the value of the structure keys is a | delimeted list so I can display
    > the actual name of the specialty which is the first list item and
    > then the count which is the second list item

    Why use a structure? Assuming these values are coming from a database, a simple count(*) query would be simpler:

    SELECT Specialty, count(*) as NumOfItems
    FROM SomeTable
    GROUP BY Specialty
    ORDER BY Specialty

    > When I cfdump the structure it's in ABC order, but when I loop
    > over it, it's not.

    Structure data is not ordered, like an array. If you want to display the data in a particular order, such as by key (ascending) you have to do it manually ie with StructSort or something similar.

    Inspiring
    March 26, 2009
    ehaemmerle wrote:
    > I have a structure called stcSpecialties. I'm trying to lllop ove the stucture
    > and display it in ABC order. When I cfdump the structure it's in ABC order but
    > when I loop over it, it's not. I'm assuming CF must be auto sorting the strcut
    > when doing a dump. How can I replicate this. I tried StructSort but I must be
    > using it wrong. Here's a sample of the structure when I cfdump it:

    <cfset t = StructNew()>
    <cfset t["Ambulance Transport"] = 3>
    <cfset t["Burn Unit"] = 4>
    <cfset sortedKeys = StructSort(t, "textnocase", "asc")>
    <cfloop from="1" to="#ArrayLen( sortedKeys )#" index="i">
    #sortedKeys # = #t[sortedKeys]#
    </cfloop>

    Structure keys can contain spaces or other characters (they're not
    variable names).

    --
    Mack