Func optional output parameters? C#

i'm trying to create a flexible insertion sort algorithm which will sort record object properties (months, days and times, mostly numeric data) so I'm passing Func delegate as a parameter to my insertion sort method, and these of course work well with my properties that are integers, but I also want to be able to perform same functionality on data such as doubles which could represent, magnitude, latitude, longitude etc.

Method overloading seems much too verbose for this instance, are there any other things I can utilize to achieve this flexibility?

Jon Skeet
people
quotationmark

It sounds like you just need to make it generic, and constrain the type to have a comparison:

public static void InsertionSort<T>(
    RecordCollection records, 
    Func<SesmicRecord, T> propertySelector)
    where T : IComparable<T>

Then just call x.CompareTo(y) when you need to compare values.

Or you could use a comparer:

public static void InsertionSort<T>(
    RecordCollection records, 
    Func<SesmicRecord, T> propertySelector, 
    IComparer<T> comparer)
{
    comparer = comparer ?? Comparer<T>.Default;
    // Now use comparer.Compare(x, y) wherever you need to compare values
}

Or just use LINQ or existing BCL classes to do the sorting unless you really need to reimplement sorting yourself for some reason...

people

See more on this question at Stackoverflow