0

I have a List(Of MyObject) that I need to sort. So I've made sure to implement IComparable, IEquatable, IEqualityComparer, and IComparer and sorting works fine.

There is new a new requirement for the project. A sorted list can not contain 'duplicate' objects. Duplicate in this case is slightly different than how duplicate is defined for sorting purposes. I've created a new routine that will check two objects and return the preferred one. The preferred one is the one that has all values the same except for one and that last value is the biggest. I've gotten this routine working well.

Now I would love to solve this 'remove duplicates' issue by creating an object that inherits IList(Of MyObject) and then overriding the Sort method to first remove 'duplicates' and then to sort using the IComparable interface as a List would normally sort itself. However, I have no idea how to do this. It doesn't even seem possible from what I've read so far. I know I could solve this by just creating a separate routine like SortWithRemove or something like that, but I was wondering if overriding is possible.

Edit: Based on the suggestions below I decided not to override Sort but to use an extension method. Here is my code:

Public Module Extensions

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub SortRemovingDuplicates(list As List(Of UniCatalogEntry))
        'filter out unwanted records
    End Sub

End Module
cjbarth
  • 4,189
  • 6
  • 43
  • 62
  • Try doing something like this post suggests: http://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq – Billy Aug 26 '11 at 13:42
  • @Billy I would, except I'm not dealing with truly duplicate records. Thanks for the suggestion though. – cjbarth Aug 26 '11 at 14:19

2 Answers2

3

Don’t change the semantics of List.Sort (I’m assuming you mean that, not IList.Sort, which doesn’t exist). You are breaking the method’s contract if you do.

If you need to remove duplicates when sorting, then write an appropriate new method for this, e.g. SortWithoutDuplicates. What you aim to do breaks the Liskov substitution principle.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I wasn't even aware of that programming concept. Thank you for pointing that out. It would make sense than that if I wanted to change what `Sort` means, I change the `IComparable` interface of my object, otherwise `Sort` should stay as it is. Thanks. – cjbarth Aug 26 '11 at 13:41
1

It may make sense to create an Extension Method to handle sorting and removing the duplicates.

public static class Extensions
{
    public static void SortAndRemoveDuplicates<T>(this IList<T> list) where T is IComparable
    {
        // add magic here...
    }
}
ShelbyZ
  • 1,494
  • 1
  • 14
  • 32
  • I would like to try this, however, I only want to extend `List(Of MyObject)`, is that possible? The compiler doesn't seem to like when I replace `T` with `Of MyObject`. – cjbarth Aug 26 '11 at 14:03
  • Ah let me update my answer for that bit. Since you said you implemented IComparable already you can use where T is IComparable and it should work. – ShelbyZ Aug 26 '11 at 14:08
  • I've included the change I made to my code in my question. This worked out very well and this extension only applies to `List(Of MyObject)`. – cjbarth Aug 26 '11 at 14:29