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.


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

Leave a Reply

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

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


A great site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: