Skip to main content
ilssac
Inspiring
September 28, 2009
Question

Aligning multiple lists

  • September 28, 2009
  • 1 reply
  • 463 views

I have 6 alphabetical lists, CFML structure key lists but I don't think this matters.

These 6 lists have many common keys with each having a few differences.  I want to display a table with each list in its own column.  I would like it to sort such that common keys align together in the same row.  But if no common key then skip that row for that list.  Maybe a diagram would clarify this.

ListA ListB ListC
1     1     1
2           2
       3     3
       4
5     5

The trouble is there is no master list to drive this from.  I just can not get my sleep deprived, aging mind to conceive of a way to create this display from six arbitrary lists.


TIA
Ian

    This topic has been closed for replies.

    1 reply

    ilssac
    ilssacAuthor
    Inspiring
    September 28, 2009

    Well I came up with one way.

    <!--- loop over and create master list of unique values by creating a temp structure --->
    <cfset masterList = structNew()>
    <cfloop collection="#positions#" item="type">
        <cfloop list="#structKeyList(positions[type])#" index="item">
          <cfset masterList[item] = "">
      </cfloop>
    </cfloop>

    <!--- extract master list --->
    <cfset masterList = listSort(structKeyList(masterList),"textNoCase")>

    <cfoutput>
    <table border="1">
        <tr>
      <cfloop list="#listSort(structKeyList(positions),'textNoCase')#" index="type">
          <th>#type#</th>
      </cfloop>
      </tr>
    <!--- loop over master list to display table of all elements ---->
    <cfloop list="#masterList#" index="key">
        <tr>
      <!--- loop over each of the contributint lists --->
      <cfloop collection="#positions#" item="type">
          <!--- if the current list has the current item, display item --->
          <td><cfif structKeyExists(positions[type],key)>#key#</cfif></td>
      </cfloop>
      </tr>
    </cfloop>
    </table>
    </cfoutput>