Structurally compare two arrays in .NET

In this post we saw how to determine if two arrays are structurally equal in .NET. Two arrays are said to be structurally equal if they contain the same elements in the same order.

Structural equality has a comparison counterpart: IStructuralComparable. It determines if an array comes before or after or is equal to another array based on the elements within it.

The Compare method returns an integer that follows the conventions of IComparer.Compare. We’ll need to override the non-generic IComparer interface.

Let’s reuse the Triangle object from the IStructuralEquatable post with a method to calculate its area:

public class Triangle
{
	public double BaseSide { get; set; }
	public double Height { get; set; }
        public double GetArea()
	{
		return (BaseSide * Height) / 2;
	}
}

We’ll compare two triangles based on their areas so we’ll implement the following comparer:

public class TriagleSizeComparer : IComparer
{
	private int Compare(Triangle x, Triangle y)
	{
		return x.GetArea().CompareTo(y.GetArea());
	}

	public int Compare(object x, object y)
	{
		return Compare((Triangle)x, (Triangle)y);
	}
}

We have two following two Triangle lists:

List triangleListOne = new List()
{
	new Triangle(){BaseSide = 2, Height = 4},
	new Triangle(){BaseSide = 4, Height = 5},
	new Triangle(){BaseSide = 5, Height = 9},
	new Triangle(){BaseSide = 3, Height = 8}
};

List triangleListTwo = new List()
{
	new Triangle(){BaseSide = 2, Height = 4},
	new Triangle(){BaseSide = 4, Height = 5},
	new Triangle(){BaseSide = 5, Height = 9},
	new Triangle(){BaseSide = 3, Height = 8}
};

Just like in the case of IStructuralEquatable we need to explicitly convert the list to an array and declare it as an interface type:

IStructuralComparable comparableArrayBase = triangleListOne.ToArray();
int compare = comparableArrayBase.CompareTo(triangleListTwo.ToArray(), new TriagleSizeComparer());

“compare” results in 0 as the two arrays are equal. You can change the base and height properties of any triangle in the array and you’ll see the following results:

  • “compare” will be 1 is triangleListOne comes first in the order
  • “compare” will be -1 if triangleListTwo comes first instead

View all various C# language feature related posts here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Structurally compare two arrays in .NET

  1. MEHMET says:

    good arc

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: