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.

Here's what my insertion sort looks like:

public static void InsertionSort(RecordCollection RecordCollection, Func<SesmicRecord, int> properyField)
{
    int sorted = 1; //how many sorted elements in array
    int index; //indexer used to point to values in array
    while (sorted < RecordCollection.Records.Length)//when sorted elements < array => Execute
    {
        SesmicRecord temp = RecordCollection.Records[sorted];//temprory value to store from array

        if (RecordCollection.SortOrder == true) //ascending
        {
            for (index = sorted; index > 0; index--)//setting index to no. of sorted elements, if > 0; => execute =>decrement
            {
                if (properyField(temp) < properyField(RecordCollection.Records[index - 1]))//when val. < val. in previous pos.
                {
                    RecordCollection.Records[index] = RecordCollection.Records[index - 1]; //set current val. to previous val. (swap)
                }
                else
                {
                    break;
                }
            }//decrement
             // reinsert value
            RecordCollection.Records[index] = temp;//reinserting deleted val in it's position
        }
        else //descending
        {
            for (index = sorted; index > 0; index--)
            {
                if (properyField(temp) > properyField(RecordCollection.Records[index - 1]))
                {
                    RecordCollection.Records[index] = RecordCollection.Records[index - 1];
                }
                else
                {
                    break;
                }
            }//decrement
             // reinsert value
            RecordCollection.Records[index] = temp;//reinserting deleted val in it's position
        }
        sorted++;//move to next element
    }
}

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