How to sort an array of structs using StructSort?

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. Sort Array
      Hi Can anyone give me a pointer on sorting this array below #curr.getRateCurrencyCode()# (lowest to highest values) <cfloop from ='1' to...
    2. 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 (
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default 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

  7. #6

    Thumbs up 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139