0

I found this post: Comparing 2 Dictionary<string, string> Instances

It's close enough to what I'm trying to do, so I thought it should work. I tried both the selected answer and the second answer and they both always return false in my case.

I'm trying to compare one pair of dictionaries and then the second pair. activeForm and activeFiles should be equal. archivedForm and archivedFiles should be equal.

enter image description here enter image description here


enter image description here enter image description here

Don't know what else I could try. Any ideas?

Community
  • 1
  • 1
sab669
  • 3,984
  • 8
  • 38
  • 75
  • Does the dictionary's value also need equal? I mean's the array's value also need same? – Sky Fang Jul 15 '15 at 12:18
  • Key and values should be identical, yes. – sab669 Jul 15 '15 at 12:20
  • All you have to do is change how those answers compare the values. Check out [`Enumerable.SequenceEqual`](https://msdn.microsoft.com/en-us/library/vstudio/bb348567(v=vs.100).aspx) for comparing arrays. – juharr Jul 15 '15 at 12:24

3 Answers3

3
public class StringArrayEqualityComparer : IEqualityComparer<string[]>
{
    public bool Equals(string[] x, string[] y)
    {
        return x.OrderBy(z => z).SequenceEqual(y.OrderBy(z => z));
    }
    public int GetHashCode(string[] obj)
    {
        return obj.GetHashCode();
    }
}

You just need to implent IEqualityComparer<T>, then use the static method answered in Comparing 2 Dictionary<string, string> Instances

Community
  • 1
  • 1
Sky Fang
  • 1,101
  • 6
  • 6
  • This will not work since the arrays could be equal as sets, but in a different order. This will fail SequenceEqual assertion. – shay__ Jul 15 '15 at 12:38
  • @shay__: Saying it "will not work" is misleading: whether the order should be considered depends on the OP's definition of equality. (Arrays are sequences, so I'd assume that ordering *should* affect equality. And if I didn't care about ordering then I'd try to make it explicit by using an `ISet` instead of `string[]`.) – LukeH Jul 15 '15 at 13:30
1

There are many ways of doing that. For example:

Dictionary exposes it's Keys as a collection. You can compare both keys collections first. If they are equal, iterate the dictionary and make sure the values are equal as well:

    private bool AreDictsEqual(IDictionary<string, string[]> d1, IDictionary<string, string[]> d2)
    {
        if (d1.Keys.OrderBy(p => p).SequenceEqual(d2.Keys.OrderBy(p => p)))
        {
            foreach (var item in d1)
            {
                if (!d2[item.Key].OrderBy(p => p).SequenceEqual(item.Value.OrderBy(p => p)))
                {
                    return false;
                }
            }
        }
        return false;
    }

There are more efficient ways of course, but that's just an example.

shay__
  • 3,815
  • 17
  • 34
  • Is that supposed to be `return true` at the very end there? And while it might not be efficient, these dictionaries will rarely be more than just a few key-value pairs. – sab669 Jul 15 '15 at 12:28
  • `SequenceEqual` will only be true if the keys are in the same order. I'm not sure that the keys are guaranteed to be ordered. – juharr Jul 15 '15 at 12:28
  • @juharr Good point. I *believe* they will be, they dictionaries are just a collection of text files and the contents (or 2 text values from the form itself). The file list is retrieved in order of Creation Date, and the text values on the form are generated from those files at load time. Basically I'm just trying to see if the form has changed. – sab669 Jul 15 '15 at 12:30
  • @sab669 Actually I just tested and the keys seem to be in the order they are added to the dictionary, so `SequenceEqual` would not work. – juharr Jul 15 '15 at 12:31
  • @juharr although I would expect them to be ordered by their hash code, and only in case of collision by insertion order. Are sure about it? – shay__ Jul 15 '15 at 12:35
  • @shay__ Check out the documentation for [`Dictionary.Keys`](https://msdn.microsoft.com/en-us/library/yt2fy5zk(v=vs.110).aspx). It specifically says _The order of the keys in the Dictionary.KeyCollection is unspecified_ – juharr Jul 15 '15 at 12:39
0

What is the code of the IEqualityComparer that compares the values? You need to compare each entry of both the arrays. Comparing the arrays directly will yield false since the references may be diferrent, even if the entries may be the same.

Bsa0
  • 2,631
  • 5
  • 17
  • 23