Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 :
 
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 24-Sep-12 22:58pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Member 9374423 at 25-Sep-12 5:24am
   
doesn't work
JF2015 at 25-Sep-12 5:25am
   
If the answer helped you, please upvote and accept it. Thanks!
Member 9374423 at 25-Sep-12 6:01am
   
Hi,It dint work
JF2015 at 25-Sep-12 6:14am
   
The above code works very well. What doesn't work for you?
Member 9374423 at 25-Sep-12 7:18am
   
I does not loop properly,for some reason.
It loop until if,then skips a line doesn't add
JF2015 at 25-Sep-12 7:54am
   
Did you copy the above code into your application and test it without modifications?
Member 9374423 at 26-Sep-12 4:39am
   
yes,then it works,but when I loop through my list it doesn't.
JF2015 at 26-Sep-12 4:41am
   
So, please share your exact code with us.
Member 9374423 at 26-Sep-12 4:47am
   
Its exactly like that,only the list is way longer and might contain whitespace characters.
JF2015 at 26-Sep-12 4:48am
   
Please post the items that don't work - I can't help you otherwise. Thanks!
Member 9374423 at 26-Sep-12 4:50am
   
"10070L0022 "
"10070L0022"
 
when comparing these two,they should be active,but it doesn't add it to the list
JF2015 at 26-Sep-12 4:59am
   
I modified the answer so that it now should ignore spaces inside the string and tested it with your values - it works now. Please try it out.
Member 9374423 at 26-Sep-12 5:05am
   
Perfect Thank you
JF2015 at 26-Sep-12 5:06am
   
I'm glad we finally got it to work!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Did you really mean this
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
?
  Permalink  
Comments
Member 9374423 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100