I am trying to write a generic method which should support intrinsic types for ex int, double, float etc. The method is sorting the array. I am getting a compile time error saying "cannot apply operator < to type T" which I understand, but how can I resolve it? Should I make the class generic and use constraints? Here is my code:
public static T[] Sort<T>(T[] inputArray)
{
for (int i = 1; i < inputArray.Length; i++)
{
for (int j = i - 1; j >= 0; j--)
{
***if (inputArray[j + 1] < inputArray[j])***
{
T temp = inputArray[j + 1];
inputArray[j + 1] = inputArray[j];
inputArray[j] = temp;
}
else
{
break;
}
}
}
return inputArray;
}