Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two distinctly different objects with similar properties

This is all in C#, using .NET 2.0.

I have two lists of objects. They are not related objects, but they do have certain things in common that can be compared such as a Guid based unique identifer. These two lists need be filtered by another list which just contains Guid's which may or may not match up with the ID's contained in the first two lists.

I have thought about the idea of casting each object list to just 'object' and sorting by that, but I'm not sure that I'll be able to access the ID property once it's cast, and I'm thinking that the method to sort the two lists should be somewhat dumb in knowing what the list to be sorted is.

What would be the best way to bring in each object list so that it can be sorted against the list with only the ID's?

like image 533
Chris Avatar asked Nov 12 '09 20:11

Chris


1 Answers

You should make each of your different objects implement a common interface. Then create an IComparer<T> for that interface and use it in your sort.

like image 56
Matthew Avatar answered Sep 30 '22 19:09

Matthew