Need to Compare 2 Copies of Complex Data Structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?c2lwcHl1Y29ubg==?=

    #1

    Need to Compare 2 Copies of Complex Data Structure

    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
  • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    #2
    RE: Need to Compare 2 Copies of Complex Data Structure

    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.

    --
    Ciaran O''Donnell



    "sippyuconn " wrote:
    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

    Comment

    • Duggi

      #3
      Re: Need to Compare 2 Copies of Complex Data Structure

      On Oct 8, 7:26 am, sippyuconn <sippyuc...@new sgroup.nospamwr ote:
      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

      Comment

      • Zhi-Xin Ye [MSFT]

        #4
        RE: Need to Compare 2 Copies of Complex Data Structure

        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:
        msdnmg@microsof t.com.

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        • =?Utf-8?B?c2lwcHl1Y29ubg==?=

          #5
          RE: Need to Compare 2 Copies of Complex Data Structure

          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(subcol l);

          Thanks


          "Zhi-Xin Ye [MSFT]" wrote:
          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:
          msdnmg@microsof t.com.
          >
          =============== =============== =============== =====
          Get notification to my posts through email? Please refer to
          http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
          =============== =============== =============== =====
          This posting is provided "AS IS" with no warranties, and confers no rights.
          >
          >

          Comment

          • Zhi-Xin Ye [MSFT]

            #6
            RE: Need to Compare 2 Copies of Complex Data Structure

            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()
            {
            InitializeCompo nent();
            }

            private void button1_Click(o bject 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(subcol l);

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

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

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

            coll2.Add(subco ll2);

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

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

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

            CompareList(col l, coll2);
            }

            ArrayList levels = new ArrayList();

            public void CompareList(Arr ayList 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.WriteLi ne("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(sub 1, sub2);
            levels.Remove(j );
            }
            }
            }
            }


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

            public int CompareTo(objec t obj)
            {
            Record r = obj as Record;
            if (r != null)
            {
            return this.value.Comp areTo(r.value);
            }
            else
            {
            throw new ArgumentExcepti on("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:
            msdnmg@microsof t.com.

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

            Comment

            Working...