0
How is an array structure sort executed?

/t5/coldfusion-discussions/how-is-an-array-structure-sort-executed/td-p/318643
Jul 12, 2007
Jul 12, 2007
Copy link to clipboard
Copied
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
<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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Guest
AUTHOR
/t5/coldfusion-discussions/how-is-an-array-structure-sort-executed/m-p/318644#M28797
Jul 12, 2007
Jul 12, 2007
Copy link to clipboard
Copied
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>
<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
<cfset ArraySwap(a, i, j) />
</cfif>
</cfloop>
</cfloop>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

