none
IstructuralComparision.CompareTo method RRS feed

  • Question

  • HI

    I am trying to understand how array.CompareTo(i.e StructuralComparer.CompareTo) method works ?Below is the code snippet please explain logic for Array.CompareTo with StructuralComparision.SturcturalCompare method passed as argumet.Why 1 is the output of below code?

     

    

        static class StructuralExtensions
    {
        public static bool StructuralEquals<T>(this T a, T b)
            where T :IStructuralEquatable
        {
            return a.Equals(b, StructuralComparisons.StructuralEqualityComparer);
        }
        public static int StructuralCompare<T>(this T a, T b)
            where T : IStructuralComparable
        {
            return a.CompareTo(b, StructuralComparisons.StructuralComparer);
        }
    }
    Output for below code is 1
       private void button1_Click(object sender, EventArgs e)
            {
      var a = new[] { 1, 3, 2};
        var b = new[] { 1, 2, 3 };
       MessageBox.Show(a.StructuralCompare(b).ToString()); // 1
         }
    Please explain me how exactly Array.ComparTo method works with StructuralComaprision.StructuralComparer and why 1 is appearing as output?
    
    Friday, January 4, 2013 9:38 AM

Answers

All replies

  • Hi Learner,

    Welcome to the MSDN Forum.

    >>

    a.StructuralCompare(b).ToString()

    This works like this:

    1. Check b is null or not, if b is null, return 1.

    2. Check the length between a and b, if they don't equals to each other, throw an exception, the exception describe that the length of b doesn't equal to a.

    3. compare the value of each array from the first element until the elements don't equal to each other or the last element of both array. I mean it compares a[0] and b[0], a[1] and b[1],a[2] and b[2] ...

    If your case:

    1. b is not null, continue to execute.

    2. a.length equals b.length, keep executing.

    3. Check the every element: a[0] = b[0], keep executing, a[1]>b[1], a is bigger than b, return 1.

    This the the result you got. I hope this is clear.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 7, 2013 5:29 AM
    Moderator
  • Thank You.

    I got it. Was just wondering what does it mean by StructuralComaprision or Why even we have IStructuralComparision interface? We can acheive same with IComparer/Icomparable.

    Monday, January 7, 2013 9:51 AM
  • Hi Learner,

    Welcome to the MSDN Forum.

    Short answer: IStructuralComparable is for collection, and IComparer is for Objects.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 7, 2013 10:32 AM
    Moderator