Saturday, February 09, 2013

List Compare in ColdFusion

In some project requirement I just wanted to compare two lists.

List 1 =  'A,a,b,c,d,e,f,g,t,h'
List 2 = 'a,c,f'

I wanted to know all elements of list 2 should be present in list 1 with a case insensitive comparison. We can do it by looping over each and every elements of list 2 and test whether it is present in list 1 or not. But, in this way if our list size will be very big then we will end up with that loop.

So, I thought do it in some good way like below.

Here in the above process,
- First we are creating a query object from the list in which we want to find our sub list.
- Doing a case insensitive filter on the query object.
- If sub list length is equal to the record count of the filtered query object then all elements of our sub list present in the list and return true. Otherwise return false.