0

I am trying to call a method on a class to sort data of a datatype that is unknown until the method is called.

Here is the code for the sort procedure that tries to call the generic method:

public void Sort(LiteTableColumn column, LiteSortOrder sortOrder)
{
    column.Sort(sortOrder);

    //create an array of the values
    object[] values = new object[_rowCount];
    for (int i = 0; i < _rowCount; i++)
    {
        values[i] = this.GetValue(column.Index, i);
    }

    Type sortType = typeof(QuickSort<>);
    Type constructedClass = sortType.MakeGenericType(Type.GetType("System." + column.DataType));

    object created = Activator.CreateInstance(constructedClass);
    _sortKey = (int[])sortType.GetMethod("Sort").MakeGenericMethod(Type.GetType("System." + column.DataType)).Invoke(constructedClass, new object[] { values });
}

I am getting an error on this line:

_sortKey = (int[])sortType.GetMethod("Sort").MakeGenericMethod(Type.GetType("System." + column.DataType)).Invoke(constructedClass, new object[] { values });

Error:

{"Int32[] Sort(T[], Test_SQL_Stream.LiteObjects.LiteSortOrder) is not a GenericMethodDefinition. MakeGenericMethod may only be called on a method for which MethodBase.IsGenericMethodDefinition is true."}

I'm not sure why exactly this is happening. Could it be because my QuickSort class has T : IComparable?

Here is the QuickSort class

public class QuickSort<T> where T : IComparable
{
    /// <summary>
    /// Sorts a given array of data in an ascending/descending fashion
    /// </summary>
    public QuickSort()
    {

    }

    /// <summary>
    /// Sorts the items in an order specifies & returns an index that reflects the sorted values
    /// </summary>
    /// <param name="sortArray">The array of values to sort (must implement IComparable)</param>
    /// <param name="direction">The order to sort. Unsorted is not a valid parameter</param>
    /// <returns></returns>
    public int[] Sort(T[] sortArray, LiteSortOrder direction)
    {
        int rows = sortArray.Length;
        int[] index = new int[rows];

        //populate the index
        for (int i = 0; i < rows; i++)
        {
            index[i] = i;
        }

        //raise an error in case the sort direction is set at unsorted and return the original list
        if (direction == LiteSortOrder.Unsorted)
        {
            new InvalidOperationException("Unable to sort items in: LiteSortOrder.Unsorted order");
            return index;
        }

        //sort the values
        quickSort(sortArray, index, 0, rows - 1);

        //if it should be descending order, reverse the items order
        if (direction == LiteSortOrder.Descending)
        {
            int t;
            int i1 = 0;
            int i2 = rows - 1;

            while (i1 < i2)
            {
                t = index[i1];
                index[i1] = index[i2];
                index[i2] = t;
                i1++;
                i2--;
            }
        }

        //return the sorted index
        return index;
    }

    private static void quickSort(T[] List, int[] RefList, int StartIdx, int EndIdx)
    {
        int Lo, Hi, T;
        T Mid;

        Lo = StartIdx;
        Hi = EndIdx;
        Mid = List[RefList[(Lo + Hi) / 2]];

        do
        {
            while (List[RefList[Lo]].CompareTo(Mid) < 0) Lo++;
            while (List[RefList[Hi]].CompareTo(Mid) > 0) Hi--;
            if (Lo <= Hi)
            {
                T = RefList[Lo];
                RefList[Lo] = RefList[Hi];
                RefList[Hi] = T;
                Lo++;
                Hi--;
            }
        } while (Lo <= Hi);
        if (Hi > StartIdx) quickSort(List, RefList, StartIdx, Hi);
        if (Lo < EndIdx) quickSort(List, RefList, Lo, EndIdx);
    }
}

EDIT: So after the comments about using Array.Sort, I looked into this method further. It is MUCH faster than my quicksort method (which I tested using strings instead of generics):

For those of you who would like to know, here are the results:

Populate Strings:159ms
Populate Objects: 53ms
Array.Sort w/Key: 10ms
Array.Sort w/o Key: 229ms
QuickSort w/strings: 231ms

and the code:

watch.Start();
        string[] values = new string[_rowCount];
        for (int i = 0; i < _rowCount; i++)
        {
            if (this.GetValue(column.Index, i) != null)
                values[i] = this.GetValue(column.Index, i).ToString();
            else
                values[i] = "";
        }
        Debug.WriteLine("Populate Strings:" +watch.ElapsedMilliseconds.ToString());
        watch.Reset();
        watch.Start();
        object[] objVal = new object[_rowCount];

        for (int i = 0; i < _rowCount; i++)
        {
            _sortKey[i] = i;
            objVal[i] = this.GetValue(column.Index, i);
        }
        Debug.WriteLine("Populate Objects: " + watch.ElapsedMilliseconds.ToString());
        watch.Reset();
        watch.Start();
        Array.Sort(_sortKey, objVal);
        Debug.WriteLine("Array.Sort w/Key: " + watch.ElapsedMilliseconds.ToString());
        watch.Reset();
        watch.Start();
        Array.Sort(objVal);
        Debug.WriteLine("Array.Sort w/o Key: " +watch.ElapsedMilliseconds.ToString());
        watch.Reset();
        watch.Start();
        QuickSort sorter = new QuickSort();
        sorter.Sort(values, LiteSortOrder.Ascending);
        Debug.WriteLine("QuickSort w/strings: " + watch.ElapsedMilliseconds.ToString());
ChandlerPelhams
  • 1,648
  • 4
  • 38
  • 56

2 Answers2

2

Your Sort method is a non-generic method on a generic class.

MakeGenericMethod can only be called on generic methods – methods that themselves take a generic parameter.
Once you construct the closed generic QuickSort<Something> type, Sort is a normal method.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

The Sort function is not generic. It is merely inside a generic class, but it is not generic itself. Hence:

 _sortKey = (int[])sortType.GetMethod("Sort").Invoke(constructedClass, new object[] { values });
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98