1 Reply Latest reply on Jul 12, 2007 9:34 AM by Hal B. Helms

    How is an array structure sort executed?

      I have created a one dimension array structure with the structure elements of id number and matches. I want to collect all of the id numbers and loop through all of the matches. When the matching criteria is finished, I would like to show the id numbers with the most matches. How do I sort a structured array? I have tried the following:

      <cfset applicant_array = arraynew(1)>
      ...start matching loop...
      <cfset applicant_array[idx] = StructNew()>
      <cfset applicant_array[idx].matches = applicant_array[idx].matches+1>
      <cfset applicant_array[idx].id = #applicant2characteristic.applicant_id#>
      ...end matching loop

      <cfset temp=#ArraySort(applicant_array, "text", "desc")#>

      The sort fails because it is on a structure. I tried using the structure element, but that fails as well. Is there a command for sorting a structure array by a specific element?


        • 1. Re: How is an array structure sort executed?
          Hal B. Helms Level 1
          So you have an array of structures, if I understand you correctly. If so, this code should work to sort that array based on the number of matches:

          <cfset a = ArrayNew(1) />
          <cfset a[1] = StructNew() />
          <cfset a[1].id = "1" />
          <cfset a[1].matches = "3" />
          <cfset a[2] = StructNew() />
          <cfset a[2].id = "2" />
          <cfset a[2].matches = "4" />
          <cfset a[3] = StructNew() />
          <cfset a[3].id = "3" />
          <cfset a[3].matches = "1" />

          <cfloop from="1" to="#ArrayLen(a)#" index="i">
          <cfloop from="#i#" to="#ArrayLen(a)#" index="j">
          <cfif a .matches LT a[j].matches>
          <cfset ArraySwap(a, i, j) />