Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How is an array structure sort executed?

Guest
Jul 12, 2007 Jul 12, 2007
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?

TIA,

Jim
TOPICS
Advanced techniques
233
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 12, 2007 Jul 12, 2007
LATEST
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.matches>
<cfset ArraySwap(a, i, j) />
</cfif>
</cfloop>
</cfloop>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources