Best way to compare two lists

I have to switch my user role into roles (list), so I’m wondering what the most efficient way to compare the two is.

If at least one match happens between the two lists I return true. Is this best made into a function in a cfc?

Only other option is looping through one list and listFind in the other. Seems like there should be a listCollision() function or something of the sort that returns matching values or a count on how many values match.

i like how coldfusion disregards empty list elements, e.g. ‘one,two,three’ has only 3 elements

too bad it can’t be told to disregard duplicate elements in the same way

so here’s an approach which doesn’t require a ListFind inside a loop, it just combines and sorts the lists and then uses a single loop to compare adjacent items

  <CFSET list3 = ListSort(Listappend(list1,list2),"text") >
 
  <CFLOOP INDEX="i" FROM="2" TO="#ListLen(list3)#">
    <CFIF ListGetAt(list3,i) EQ ListGetAt(list3,i-1) >
      <CFOUTPUT>
        <br>list3 has duplicate entry #ListGetAt(list3,i)# 
      </CFOUTPUT>
    </CFIF>
  </CFLOOP>

helps?

interesting, thank you.

Convert both lists to arrays using ListToArray(), use ArraySort() then compare them as strings. Should work :slight_smile: