Need to Compare 2 Copies of Complex Data Structure

S

sippyuconn

Hi

I have a Container that is an an Array List of Class
Each ArrayList element can be the class or a another ArrayList of Class
So there the ArrayList could look like

Element 1 - Class
Element 2 - Class
Element 1 - ArrayList Of Class
Element 1 - Class
Element 2 - Class
Element 3 - Class
Element 4 - Class

I put elements on buy recursively going thru elements

Now I need to compare 2 of these Items and determine the difference

Any ideas on how to best achieve this ???

I was thinking of doing it recursively but I need to keep the 2 items at the
same point to compare element by elmement and wasn't sure how to do that ???

Thanks
 
C

Ciaran O''Donnell

You need to establish the rules of the comparison. Then implement IComparable
on the class and possibly create a subclass of arraylist to then implement
IComparable on there.
If you give a brief description of the rules for comparison then I would be
able to give you a bit of sample code.
 
D

Duggi

Hi

I have a Container that is an an Array List of Class
Each ArrayList element can be the class or a another ArrayList of Class
So there the ArrayList could look like

Element 1 - Class
Element 2 - Class
Element 1 - ArrayList Of Class
                    Element 1 - Class
                    Element 2 - Class
Element 3 - Class
Element 4 - Class

I put elements on buy recursively going thru elements

Now I need to compare 2 of these Items and determine the difference

Any ideas on how to best achieve this ???

I was thinking of doing it recursively but I need to keep the 2 items at the
same point to compare element by elmement and wasn't sure how to do that ???

Thanks

I really do not understand your question. However from the subject
line I think you wanted to compare two different datastructures?

If so, Please explore IComparable , overloading == and also explore
Equals method of object, which can give you a good knowledge how to
procede with your issue.

-Cnu
 
Z

Zhi-Xin Ye [MSFT]

Hi sippyuconn,

Thank you for contacting Microsoft Managed Newsgroup Support Service, I'm
Zhi-Xin and I'm assigned to help you with this issue.

First I would like to confirm my understanding with you. Do you mean you
have two ArrayList of a custom class, and you want to compare these two
lists element by element to determine the difference? Are the two
ArrayList of the same length?

I look forward to hearing from you.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sippyuconn

Hi

Yes they are the same structure/length only values in elements maybe differnt
I basically clone the original object then make change a value or 2 then I
need to find difference

Here example

public class Record
{
public bool IsField = true;
public bool IsSubCollection = false;

public string value = "";
}

public class MainCollection : ArrayList
{

}

public class SubCollection : ArrayList
{

public bool IsField = false;
public bool IsSubCollection = true;
}

MainCollection coll = new MainCollection();
SubCollection subcoll = new SubCollection();


for(int i = 0; i < 5; i++)
{
Record rec = new Record();
subcoll.Add(rec);
}

for (int i = 0; i < 5; i++)
{
Record rec = new Record();
coll.Add(rec);
}

coll.Add(subcoll);

Thanks
 
Z

Zhi-Xin Ye [MSFT]

Hi sippyuconn,

Do you mean you have two list of MainCollection, and their structure and
length are of the same?
If so, you can implement the IComparable interface on the Record class,
write your comparation rule in the CompareTo() method.
Then you can write a compare function to loop through the lists to compare
the elements:

- If the element is Record object, call the CompareTo() method to compare
the differences;
- If the element is SubCollection object, convert the element to
SubCollection object and call the compare function recursively;

I write the following sample for your information:

namespace Sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
MainCollection coll = new MainCollection();
SubCollection subcoll = new SubCollection();

//======= Main collection 1 =====
for (int i = 0; i < 5; i++)
{
Record rec = new Record();
subcoll.Add(rec);
}

for (int i = 0; i < 5; i++)
{
Record rec = new Record();
coll.Add(rec);
}

coll.Add(subcoll);

//======= Main collection 2 =====

MainCollection coll2 = new MainCollection();
SubCollection subcoll2 = new SubCollection();
for (int i = 0; i < 5; i++)
{
Record rec = new Record();
subcoll2.Add(rec);
}

for (int i = 0; i < 5; i++)
{
Record rec = new Record();
coll2.Add(rec);
}

coll2.Add(subcoll2);

//==== Change some element ====

Record r = (Record)coll2[0];
r.value = "test";

((Record)((SubCollection)coll2[5])[2]).value = "something";

CompareList(coll, coll2);
}

ArrayList levels = new ArrayList();

public void CompareList(ArrayList arr1, ArrayList arr2)
{
for (int j = 0; j < arr1.Count; j++)
{
if (arr1[j] is Record)
{
Record rd1 = (Record)arr1[j];
Record rd2 = (Record)arr2[j];
if (rd1.CompareTo(rd2) != 0)
{
levels.Add(j);

string pos = "";
foreach (int ls in levels)
{
pos += ls.ToString() + "-";
}

Console.WriteLine("Elements at position: {0} are
different. The values are \"{1}\" vs \"{2}\"",
pos, rd1.value, rd2.value);

levels.Remove(j);
}
}
if (arr1[j] is SubCollection)
{
levels.Add(j);
SubCollection sub1 = (SubCollection)arr1[j];
SubCollection sub2 = (SubCollection)arr2[j];
//If the element is SubCollection object, call the
compare function recursively;
CompareList(sub1, sub2);
levels.Remove(j);
}
}
}
}


public class Record : IComparable
{
public string value = "";

public int CompareTo(object obj)
{
Record r = obj as Record;
if (r != null)
{
return this.value.CompareTo(r.value);
}
else
{
throw new ArgumentException("Object is not Record!");
}
}
}

public class MainCollection : ArrayList
{
}

public class SubCollection : ArrayList
{
}
}


The output of the sample is as follows:

Elements at position: 0- are different. The values are "" vs "test"
Elements at position: 5-2- are different. The values are "" vs "something"

If any of my solution is not clear, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top