6

I have everything in a Generic Linked list done except for sorting. And I don't know how to use IComparable or how I would go about doing this because it's generic. I don't know what I would even be comparing or sorting?

public class Node<T> : IComparable<T>
{
    private Node<T> next;
    private T item;

}

So

public int CompareTo( T other )
{
    // TODO: Find out how to do it properly
    throw new NotImplementedException();
}

It is also against the instructions to convert it to an array and then sort it, then convert it back.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Technocrat
  • 334
  • 1
  • 4
  • 15
  • 1
    you have to write a method that compares one Node with another. YOu can probable simply forward the call the T.CompareTo of the contained item – pm100 Aug 18 '14 at 20:48
  • [Implementing IComparable](http://stackoverflow.com/questions/1894399/implementing-icomparable) – MethodMan Aug 18 '14 at 20:50

2 Answers2

6

Having a linked list Node object implement IComparable makes absolutely no sense for the exact reasons you describe. Instead, the classes that you use in the linked list should implement it. In fact, you can require this with a generic type constraint:

MyClass<T> where T : IComparable<T> {}

With that done, you can use T as if it were an IComparable when performing your sort.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • This is limiting. You might want to make a linked list of Customer objects that can't implement `IComparable` because there is no obvious way to order customers, but you might also want to sort a list of customers, sometimes by name, sometimes by postal code, and sometimes by total value of their orders. Instead, the sort method should take a comparison delegate or an IComparer implementation and use that. – phoog Aug 18 '14 at 22:27
  • @phoog I would agree that the delegate approach is more standard/better. You could make it even easier and implement `IEnumerable` instead and use the LINQ versions instead. An excellent point though. – BradleyDotNET Aug 18 '14 at 22:46
2

The first thing you ought to do is to read up on sort algorithms and decide which one you're going to use. Once you've done that, you can worry about comparing your generic values.

If you want to follow the framework approach, don't require your T to implement IComparable<T>. Instead, use Comparer<T>.Default. This approach allows you to write your class to support user-defined comparisons:

public class LinkedList<T>
{
    public void Sort() { this.Sort(Comparer<T>.Default); }
    public void Sort(IComparer<T> comparer)
    {
        //todo: implement
        throw new NotImplementedException();
    }
}

My initial version of this answer had the comparer as a property of the class, but that's really incorrect, because a linked list is not an inherently sorted type. You might want to sort the list one way once, and then 2 seconds later sort it a different way. The comparer should therefore be a parameter of the sort method.

phoog
  • 42,068
  • 6
  • 79
  • 117