Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
If you have two lists(array lists for example),How can I compare the values of the one list against all the values of another list.
example :

VB
dim activeList as new ArrayList

dim list1 as new ArrayList

list1.add("this")
list1.add("That")
list1.add("Another")
list1.add("Value")

dim list2 as new ArrayList

list2.add("Anotgher")
list2.add("ok")
list2.add("this")
list2.add("Hi")


so what need to happen is.
Every value in list one has to be checked if it is in any index of list2.
and the added to activeList.

I just need that loop or comparison script.
Posted

Hi,

try this code - I modified it to ignore spaces inside the strings:
Dim list1 As New ArrayList()
Dim list2 As New ArrayList()
Dim activeList As New ArrayList()

list1.Add("this")
list1.Add("That")
list1.Add("Another")
list1.Add("Value")
list1.Add("10070L0022 ")
list1.Add("10070L0023")

list2.Add("Another")
list2.Add("ok")
list2.Add("this")
list2.Add("Hi")
list2.Add("10070L0022")
list2.Add("10070L0023 ")
For Each o As Object In list1
	For Each o2 As Object In list2
		If DirectCast(o, String).Replace(" ", "") = DirectCast(o2, String).Replace(" ", "") Then
			activeList.Add(o)
		End If
	Next
Next
 
Share this answer
 
v2
Comments
Member 9374423 25-Sep-12 5:24am    
doesn't work
JF2015 25-Sep-12 5:25am    
If the answer helped you, please upvote and accept it. Thanks!
Member 9374423 25-Sep-12 6:01am    
Hi,It dint work
JF2015 25-Sep-12 6:14am    
The above code works very well. What doesn't work for you?
Member 9374423 25-Sep-12 7:18am    
I does not loop properly,for some reason.
It loop until if,then skips a line doesn't add
Did you really mean this
VB
For Each s1 As String In list1
  For Each s2 As String In list2
    If s1 = s2 Then
      activeList.Add(s1)
    End If
  Next
Next

?
 
Share this answer
 
Comments
Member 9374423 25-Sep-12 5:24am    
does not work,It loops forever

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900