Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
gnesland #1
How to sort an array of structs using StructSort?
How can I sort an array of structs using StructSort (or another method) and
using a struct element as the sorting key?
I have an array of structs, where each struct contains a result record after a
query using Verity Search.
The array documentArray contains structs called documentStruct.
The structs contain the elements
- documentStruct.ID
- documentStruct.Title
- documentStruct.Score
- documentStruct.SourceDB.
The array should be sorted by the documentStruct.Score element independent of
where the records come from, but since the records inserted are coming from
different databases the structs are sorted by score for each DB and not by the
?overall score?.
How can I use StructSort or another method/function to solve this?
(If the description was unclear, please inform me)
gnesland Guest
-
Sort Array
Hi Can anyone give me a pointer on sorting this array below #curr.getRateCurrencyCode()# (lowest to highest values) <cfloop from ='1' to... -
sort array by key
Hi, What I want is simple, but I can't figure it out at the moment. Let's say this is an array names $matches: Array ( => Array ( -
Sort a 2D array
Hello, I have a 2D array which I would like to sort. I take a simple example: $tab = 'toto';$tab = 'toto'; $tab = 'aaaa';$tab = 'titi'; $tab... -
sort w/o using an array
I am trying to figure out if there is a way to do a sort that doesn't involve putting an entire file in memory. This kind of thing is available in... -
Help me sort a two - d array
I need help sorting a multidimensional array. I have an array myarray(column_no, row_no) it has 5 columns for this example I have used 4 rows... -
parvee #2
Re: How to sort an array of structs using StructSort?
Hi,
You could use structure of structures instead of array of structures and use
StructSort. See the examples provided by MM for using StructSort -
pathToSubElement to do this.
I hope this helps:light;
parvee Guest
-
gnesland #3
Re: How to sort an array of structs using StructSort?
Hi.
Thanks for the tip.
But what function and syntax do I use to organize several structures within an
superior structure? The documentation and examples of MM does not explain the
idea of a structure of structures.
gnesland Guest
-
mxstu #4
Re: How to sort an array of structs using StructSort?
I think parvee is referring to something like the attached. It's much simpler
than it looks. The actual sort is a one-liner, and the rest of the code is
just creating/showing the sample data.
See [url]http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/funca104.htm[/url]
<cfset allDocumentsStruct = StructNew()>
<cfset documentStruct = StructNew()>
<cfset documentStruct.ID = "STRING1">
<cfset documentStruct.Title = "Item A">
<cfset documentStruct.Score = 85>
<cfset documentStruct.SourceDB = "Database A">
<cfset allDocumentsStruct[documentStruct.ID] = documentStruct>
<cfset documentStruct = StructNew()>
<cfset documentStruct.ID = "STRING2">
<cfset documentStruct.Title = "Item B">
<cfset documentStruct.Score = 99>
<cfset documentStruct.SourceDB = "Database A">
<cfset allDocumentsStruct[documentStruct.ID] = documentStruct>
<cfset documentStruct = StructNew()>
<cfset documentStruct.ID = "STRING3">
<cfset documentStruct.Title = "Item C">
<cfset documentStruct.Score = 62>
<cfset documentStruct.SourceDB = "Database B">
<cfset allDocumentsStruct[documentStruct.ID] = documentStruct>
<cfset documentStruct = StructNew()>
<cfset documentStruct.ID = "STRING4">
<cfset documentStruct.Title = "Item D">
<cfset documentStruct.Score = 87>
<cfset documentStruct.SourceDB = "Database C">
<cfset allDocumentsStruct[documentStruct.ID] = documentStruct>
<cfset sortedKeys = StructSort(allDocumentsStruct, "numeric", "desc",
"score")>
<cfoutput>
<table border="1">
<cfloop from="1" to="#ArrayLen(sortedKeys)#" index="i">
<tr>
<cfset currStruct = allDocumentsStruct[sortedKeys[i]]>
<td>#currStruct.Score#</td>
<td>#currStruct.Title#</td>
<td>#currStruct.SourceDB#</td>
</tr>
</cfloop>
</table>
</cfoutput>
mxstu Guest
-
gnesland #5
Re: How to sort an array of structs using StructSort?
GENIAL!
Thank you, thank you, thank you!
This worked perfectly :)
And really nice of you to include the surrounding code as well, and not just
the line of code for sorting the data. This makes it a lot easier to understand
the entirety.
Have a fantastic day!
gnesland Guest
-
Unregistered #6
Re: How to sort an array of structs using StructSort?
Thanks!!! There is no way I could figure that out myself! REALLY appreciate it!
Unregistered Guest



Reply With Quote

