Skip to main content
September 23, 2008
Answered

list comparisons

  • September 23, 2008
  • 6 replies
  • 635 views
Whats the most EFFICIENT way of comparing 2 lists to know if they are the same or not? I dont care about what is different about them...truly. I just want to know if there is even one difference between them, then send me back a notification that they are not the same.
It's easy when they are not the same length and listlen would help.
But when they are the same length, is looping the ONLY solution? Wouldn't that be bothersome if many items in the list? I wish CF would build a simple CF tag to do this :)!
    This topic has been closed for replies.
    Correct answer Kronin555
    <cfif listlen(list1) eq listlen(list2) and listsort(list1,"text") eq listsort(list2,"text")>
    Lists are the same!
    <cfelse>
    Lists are different!
    </cfif>

    After you do a listsort, you can just treat the returned list as a string and do a string equality comparison with the other sorted list. Note that, depending on the sorting algorithm that CF uses internally, those ListSort calls can take n or log(n) time. If you can guarantee that the lists you are comparing are always pre-sorted, you can remove that part.

    Update: I just re-read your last post... in the case where a list contains a multiple, are you wanting the multiple to also occur in the second list to say they are equal? or are you just looking for the same items, but not necessarily the same count of each item?

    6 replies

    September 23, 2008
    Ian and Kronin~
    Thank you very much! Today was the first time ever I used a forum and have realised what a nice asset it is! You guys have been really helpful!! I will be using it without the hash and it just worked great!
    Thanks guys!
    Take care
    Inspiring
    September 23, 2008
    I_LoveCF wrote:
    > Ian and Kronin~
    > Ian: If you see Kronin's reply, he has almost the exact same suggestion like
    > you have above with :
    >
    > hash(listSort(ucase('ant,cow,hen')),'text')
    > Kronin is just not using the hash...
    >
    > Can I ask the reason behind the hashing logic?

    Because I was adapting a technique where I was comparing large sets of
    text, but did not want to store the whole amount for future comparisons.
    The hash() let me condense entire text files into a few dozen characters.

    With shorter bodies of text, it is unnecessary and Kronin's will have
    similar results. Also in either solution the len() comparison is
    redundant and unnecessary.


    Participating Frequently
    September 23, 2008
    > in either solution the len() comparison is redundant and unnecessary.

    In cases where the lists aren't the same length, it short-circuits the listsort and string equality checks. If the lists will always be relatively short, then I agree, it's probably overkill. If, however, the lists have the possibility of being fairly long (on the order of 100s of elements), then the listlen check can speed up comparisons when the lists are different. If the lists match, then it actually adds a little processing time.
    September 23, 2008
    Ian and Kronin~
    Ian: If you see Kronin's reply, he has almost the exact same suggestion like you have above with :

    hash(listSort(ucase('ant,cow,hen')),'text')
    Kronin is just not using the hash...

    Can I ask the reason behind the hashing logic?
    Otherwise thanks guys! I think I'm almost all done once I get a reply from Ian on the hashing...
    Thanks!

    Inspiring
    September 23, 2008
    I_LoveCF wrote:
    > Hi Ian~
    > Thanks for replying! Nope, as far as the task that I'm working on, I dont care
    > about the order, I only care to see if the lists are different:

    So does that mean that you want this function to return YES the lists
    are the same for red,green,blue and blue,red,green OR NO they are different?

    If you just want to compare to strings to ensure they are exactly the
    same, the hash function does this nicely.

    <cfif hash('ant, cow, hen') EQ hash('hen, ant, dog')>
    THEY ARE THE SAME
    <cfelse>
    THEY ARE DIFFERENT
    </cfif>

    If you want to eliminate list order and case you can the sort function
    and case function to make them more similar.

    hash(listSort(ucase('ant,cow,hen')),'text') EQ
    hash(listSort(ucase('hen,ant,dog')),'text')
    Participating Frequently
    September 23, 2008
    Hash() is not necessary here, and actually adds processing time, not decreases it. It doesn't add much, but why do it if it's not necessary?

    On my machine, running the attached code sample, simple string comparison (not using Hash()) takes 44 ticks. With Hash(), it takes 66 ticks.
    September 23, 2008
    Hi Ian~
    Thanks for replying! Nope, as far as the task that I'm working on, I dont care about the order, I only care to see if the lists are different:
    For example:
    List 1: ant, cow, hen
    List 2: bull, dog

    My listlen check will immediately tell me these are not the same (which is all the info I'm looking for).

    I struggle with cases like this:
    List 1: ant, cow, hen
    List 2: hen, ant, dog

    Same length but one different item or multiples.
    I just need to know here that List 1 <> List 2.

    Thanks for your help
    Kronin555Correct answer
    Participating Frequently
    September 23, 2008
    <cfif listlen(list1) eq listlen(list2) and listsort(list1,"text") eq listsort(list2,"text")>
    Lists are the same!
    <cfelse>
    Lists are different!
    </cfif>

    After you do a listsort, you can just treat the returned list as a string and do a string equality comparison with the other sorted list. Note that, depending on the sorting algorithm that CF uses internally, those ListSort calls can take n or log(n) time. If you can guarantee that the lists you are comparing are always pre-sorted, you can remove that part.

    Update: I just re-read your last post... in the case where a list contains a multiple, are you wanting the multiple to also occur in the second list to say they are equal? or are you just looking for the same items, but not necessarily the same count of each item?
    Inspiring
    September 23, 2008
    I_LoveCF wrote:
    > I wish CF would build a simple CF tag
    > to do this :)!
    >

    Does order matter? I.E. is red,green,blue that same as blue,green,red?