Reputation: 11588
My question is like this one: c# list compare
but the only thing to note is this:
I'm using .NET Framework 2.0
So how can I compare two lists on C# framework 2 and return a boolean value if the items are different?
instance == anotherone fails
instance.Equals(anotherone) fails.
Edit:
They are both List
Edit 2
I'm trying to compare if the list values are exactly. I can sort them, np for that. The problem is if the count or the values of the items changes. For example:
List1->Item1 = "A"
List1->Item2 = "B"
List2->Item1 = "B"
List2->Item2 = "A"
//must return true
List1->Item1 = "A"
List1->Item2 = "B"
List2->Item1 = "B"
List2->Item2 = "C"
//must return false
List1->Item1 = "A"
List1->Item2 = "B"
List2->Item1 = "B"
List2->Item2 = "A"
List2->Item3 = "A"
//must return false, etc.
Thanks and kind regards.
Upvotes: 0
Views: 4193
Reputation: 7054
Leandro,
You could also use my open source Compare .NET Objects Library. You would set the Config.IgnoreCollectionOrder to true.
https://comparenetobjects.codeplex.com/
[Test]
public void ComparerIgnoreOrderSimpleArraysTest()
{
var a = new String[] { "A", "E", "F" };
var b = new String[] { "A", "c", "d", "F" };
var comparer = new CompareLogic();
comparer.Config.IgnoreCollectionOrder = true;
comparer.Config.MaxDifferences = int.MaxValue;
ComparisonResult result = comparer.Compare(a, b);
Console.WriteLine(result.DifferencesString);
Assert.IsTrue(result.Differences.Where(d => d.Object1Value == "E").Count() == 1);
Assert.IsTrue(result.Differences.Where(d => d.Object2Value == "c").Count() == 1);
Assert.IsTrue(result.Differences.Where(d => d.Object2Value == "d").Count() == 1);
}
Upvotes: 1
Reputation: 241651
For the question that you link to on computing the intersection, you would need to implement your own version of Intersect
. This should get you started:
List<T> Intersect<T>(List<T> first, List<T> second) {
Dictionary<T, T> potential = new Dictionary<T, T>();
foreach (var item in first) {
potential.Add(item, item);
}
List<T> intersection = new List<T>();
foreach (var item in second) {
if (potential.Remove(item)) {
intersection.Add(item);
}
}
return intersection;
}
To handle if they have the same items with the same frequency:
bool AreSameAsMultiSets(List<T> first, List<T> second) {
Dictionary<T, int> counts = new Dictionary<T, int>();
foreach (var item in first) {
if (!counts.ContainsKey(item)) {
counts.Add(item, 0);
}
counts[item] = counts[item] + 1;
}
foreach (var item in second) {
if (!counts.ContainsKey(item)) {
return false;
}
counts[item] = counts[item] - 1;
}
foreach (var entry in counts) {
if (entry.Value != 0) {
return false;
}
}
return true;
}
You should probably add some error-handling to the above (first is not null, second is not null). Note that you can't use HashSet<T>
since you're in .NET 2.0.
Upvotes: 5
Reputation: 700422
If you want to check if the lists contain identical items (i.e. the same items in the same order):
public static bool ListsEqual<T>(List<T> list1, List<T> list2) {
if (list1.Count != list2.Count) return false;
for (int i = 0; i < list1.Count; i++) {
if (list1[i] != list2[i]) return false;
}
return true;
}
Upvotes: 4