[RESOLVED] Compare two lists/collections
What i want to do is compare two lists, and then view which items have been added/removed.
So, for example:
List1:
bob
jack
tim
dave
john
Compared with list2:
bob
alan
jack
tim
george
john
would show that alan and george were added and that dave was removed.
How would i go about this?
Re: Compare two lists/collections
I don't know of an easy way to do this, as you need to go through all the items in list 1 and see if list 2 contains them to find what was deleted, then you have to go through all the items in list 2 and see if list 1 contains them to see what was added. If you are only doing this every now and then, and the lists aren't too excessively big, then that should be fine, it's just two loops, one run right after the other using the IndexOf or Contains methods of the List object.
However, if this is going to get run often, then it would probably be better to make up a structure which holds the main item, plus a couple boolean values such as Deleted. When something is deleted, rather than removing it from the list, you just toggle the Deleted member of the structure. This would cut out one of the loops from the earlier list. By adding a second boolean, and doing something a bit more tricky with it, you could get rid of the first list, too, but I'll stop there, because it may be way different from your needs.
Re: Compare two lists/collections
Loop through the first one and check the List2.Contains for that string. That will get you anything that was removed when it returns false. Then loop through the second one and check the List1.Contains. That will get you anything that was added when it returns false.
Re: Compare two lists/collections
Re: Compare two lists/collections
Ok, well what I exactly want it for is to compare the contents of a folder, with a previous list of files from that folder so I can see which files have been deleted/created.
I tried to use the FileSystemWatcher, but the way that the files are deleted does not create the even that the watcher looks for.
(Specifically, the way that Firefox deletes its x.x.part files when finishing downloading large files)
Just wondering if there is another way to do this in that case if looping through the folders contents and comparing is going to be 'effort'.
Thanks.
Re: Compare two lists/collections
How often do you expect that code to run? It's not all that imposing a task, but it could cause trouble if it was going to happen once a second.
Re: Compare two lists/collections
I guess ill run it every 30'secs or so just to check when the download is finished. Could you think of any other ways to do this?
Re: Compare two lists/collections
Nothing that would have a huge impact. You might look at properties of the files, perhaps the last edited date would be of some value. Also, you might look at the total count of files. One addition and one deletion would result in the same total number of files, so just looking at the number would not be a totally reliable test, but it would be a quick test.
I don't know of a much better way to do things, but it sounds like you have some code written to take a snapshot of the folder by making a list of files that are in the folder. Couldn't you alter that code such that when the list is being populated, it checks the old list to see if the file is already there, and if it is, you have your addition. That would cut the comparison down to just searching for deletions. It really shouldn't be too costly in time.
Re: Compare two lists/collections
Im just using the My.Computer.FileSystem.GetFiles function.
Ive written the code now, and im just tweaking it a bit, it runs fine on my PC and thats the only place that its intended for so it should all be ok.
Thanks for you help, my code looks horrible but its working so thats good enough for me! :)
Re: [RESOLVED] Compare two lists/collections
If you have VS 2008 you can use Linq to compare 2 Lists.
Take a look at this Video. I think it will help you to understand who you can do this with a linq query