I'm working on a primitive quicksort algorithm using LINQ and generics but I've run into some issues, mainly with type inference.
Here is the code
private static List<T> Quicksort<T>(IEnumerable<T> list)
where T : IComparable, IComparable<T>
{
if (!list.Any())
return new List<T>();
var first = list.First();
var smallerAndLarger = list.Skip(1).GroupBy(n => n >= first);
return new List<T>
(Quicksort(smallerAndLarger.Where(x => !x.Key)).Add(first))
.AddRange(Quicksort(smallerAndLarger.Where(x => x.Key)));
}
The compiler is complaining about this line
var smallerAndLarger = list.Skip(1).GroupBy(n => n >= first);
Saying that the type T
will not support comparison operations, but I have already set in the method signature that T
should implement IComparable
and the generic version so this should work?