Search code examples
c#design-patternsarchitectureoop

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 identifier. These two lists need to be filtered by another list which just contains GUIDs which may or may not match up with the IDs 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 IDs?


Solution

  • 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.