1

I have two Lists of Lists of int

var a = new List<IList<int>>();
var b = new List<IList<int>>();

Each of them having the following data:

var a = new List<IList<int>>()
                {
                    new List<int>() { 1, 2 },
                    new List<int>() { 4, 5, 6 },
                };

var b = new List<IList<int>>()
                {
                    new List<int>() { 6, 5, 4 },
                    new List<int>() { 2, 1 },
                };

I want to treat a and b like sets of sets so, upon a.Equals(b), it should return true.

How can I do my Equals method?

halfer
  • 19,824
  • 17
  • 99
  • 186
SuperJMN
  • 13,110
  • 16
  • 86
  • 185

2 Answers2

3

Assuming your check needs to be order ignorant, you should check this out: LINQ : Determine if two sequences contains exactly the same elements.

A set of sets IEqualityComparer implementation might look like so:

public bool Equals(List<IList<int>> x, List<IList<int>> y)
{
    foreach(var innerList in x)
    {
        var innerSet = new HashSet<int>(innerList);
        var hasEquivalent = false;

        foreach(var otherInnerList in y)
        {
            hasEquivalent = innerSet.SetEquals(otherInnerList);
            if(hasEquivalent) break;
        }

        if(!hasEquivalent) return false;
    }

    return true;
}
Mihai Pantea
  • 360
  • 3
  • 8
  • Please, take into account that I'm not comparing 2 lists, but 2 lists of lists and that I want to determine equality in both levels of the nesting. – SuperJMN Aug 08 '17 at 08:39
  • I've updated the answer to account for sets of sets, assuming each inner set has the same fixed size, and that an inner set on one side needs to have an equivalent on the other side – Mihai Pantea Aug 08 '17 at 09:10
  • Sorry, but for my problem I cannot assume that each has a fixed size. See my updated question (the data). It should work with the data in the sample. – SuperJMN Aug 08 '17 at 09:14
  • 1
    I may have expressed myself poorly. I wanted to clarify that `new List() { 1, 2 }, new List() { 4, 5, 6 } }` won't be equivalent to `new List() { 1, }, new List() { 2, 4, 5, 6 }}` I meant to say that the inner sets will look for equivalents in the subset of sets of the same size they are. – Mihai Pantea Aug 08 '17 at 09:18
1

One way of doing it without using linq to check foreach element would be First create an EqualityComparer

class ListComparer : IEqualityComparer<IList<int>>
{
    public bool Equals(IList<int> x, IList<int> y)
    {
      return  x.SequenceEqual(y);
    }

    public int GetHashCode(IList<int> obj)
    {
        throw new NotImplementedException();
    }

}

then compare the two elements using the equalitycomparer

var equals=  one.SequenceEqual(two,new ListComparer());
npo
  • 1,060
  • 8
  • 9
  • Please, take into account that I'm not comparing 2 lists, but 2 lists of lists and that I want to determine equality in both levels of the nesting. – SuperJMN Aug 08 '17 at 08:40
  • The code will go like this, it will compare each element of the list one with list two, when comparing listone and listtwo it will use the ListComparer with intern treats the elements of the listone and listtwo as lists and compares them as well as lists, the code could also be modified so it would compare a list of lists of list of lists .......... of lists by changing public bool Equals(IList x, IList y) { return x.SequenceEqual(y,new ListComparer()); } – npo Aug 08 '17 at 09:19