tags:

views:

878

answers:

6

I have a List (Foo) and I want to see if it's equal to another List (foo). What is the fastest way ?

+1  A: 

Something like this:

public static bool CompareLists(List<int> l1, List<int> l2)
{
 if (l1 == l2) return true;
 if (l1.Count != l2.Count) return false;
 for (int i=0; i<l1.Count; i++)
  if (l1[i] != l2[i]) return false;
 return true;
}

Some additional error checking (e.g. null-checks) might be required.

M4N
You could also make it "more generic" and also use a "comparer" instead of "!=".
Fabrizio C.
+9  A: 

Here are the steps I would do:

  1. Do an object.ReferenceEquals() if true, then return true.
  2. Check the count, if not the same, return false.
  3. Compare the elements one by one.

Here are some suggestions for the method:

  1. Base the implementation on ICollection. This gives you the count, but doesn't restrict to specific collection type or contained type.
  2. You can implement the method as an extension method to ICollection.
  3. You will need to use the .Equals() for comparing the elements of the list.
vboctor
A: 

Something like this maybe using Match Action.

public static CompareList<T>(IList<T> obj1, IList<T> obj2, Action<T,T> match)
{
   if (obj1.Count != obj2.Count) return false;
   for (int i = 0; i < obj1.Count; i++)
   {
     if (obj2[i] != null && !match(obj1[i], obj2[i]))
       return false;
   }
}
Yoann. B
It already exist IComparer (http://msdn.microsoft.com/en-us/library/tfakywbh.aspx) and Comparison<T> (http://msdn.microsoft.com/en-us/library/tfakywbh.aspx).You can't use Action<T, T> because it returns void.
Fabrizio C.
I'm sorry: IComparer<T> is at http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx .
Fabrizio C.
+13  A: 

From 3.5 onwards you may use a LINQ function for this:

List<string> l1 = new List<string> {"Hello", "World","How","Are","You"};
List<string> l2 = new List<string> {"Hello","World","How","Are","You"};
Console.WriteLine(l1.SequenceEqual(l2));

It also knows an overload to provide your own comparer

flq
" That's hot! "
frou
You will need to implement an IEqualityComparer to compare lists of a custom class, but its easy and this MS page explains how: http://msdn.microsoft.com/en-us/library/bb348567.aspx
Michael La Voie
Outstanding. I only needed to implement IEquatable<T> on my objects as I only cared about the data in the objects
BozoJoe
A: 

Assuming you mean that you want to know if the CONTENTS are equal (not just the list's object reference.)

If you will be doing the equality check much more often than inserts then you may find it more efficient to generate a hashcode each time a value is inserted and compare hashcodes when doing the equality check. Note that you should consider if order is important or just that the lists have identical contents in any order.

Unless you are comparing very often I think this would usually be a waste.

Chris Nava
A: 

One shortcut, that I didn't see mentioned, is that if you know how the lists were created, you may be able to join them into strings and compare directly.

For example...

In my case, I wanted to prompt the user for a list of words. I wanted to make sure that each word started with a letter, but after that, it could contain letters, numbers, or underscores. I'm particularly concerned that users will use dashes or start with numbers.

I use Regular Expressions to break it into 2 lists, and them join them back together and compare them as strings:

    var testList = userInput.match(/[-|\w]+/g)  
        /*the above catches common errors: 
         using dash or starting with a numeric*/
    listToUse = userInput.match(/[a-zA-Z]\w*/g)

    if (listToUse.join(" ") != testList.join(" ")) {
                return "the lists don't match"

Since I knew that neither list would contain spaces, and that the lists only contained simple strings, I could join them together with a space, and compare them.