Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6847911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:48:09+00:00 2026-05-27T00:48:09+00:00

I am trying to call a method on a class to sort data of

  • 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());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:48:10+00:00Added an answer on May 27, 2026 at 12:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a Sort method that expects a parameter of type IComparer<object>
I'm trying to call overloaded method in code like this: public abstract class BaseClass<T>
I have an issue here where I'm trying to call a class method on
I'm trying to intercept a method call (afterInsert() of a domain class) in a
I am trying to make a call to a Parent class method from a
I am working with ASP.net. I am trying to call a method that exists
I am trying to call a class method dynamically depending on a condition. This
I'm trying to call a method of another class but this causes my app
When trying to call on a method from another class my method as a
I'm trying to call the java code from S. This method call: cls =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.