so here's my code
public void BubbleSort<T>(T[] array) where T : IComparable<T>
{
for (int i = 0; i < array.Length; i++)
{
for (int j = 1; j < array.Length; j++)
{
if (array[j] < array[j - 1])
{
}
}
}
}
and before you shoot be down for not searching. I have searched and one of the answers here on SO said to use an icomparable interface to solve the problem.
unfortunately i am not going anywhere with this error.
It looks like you're expecting the IComparable<T>
constraint to allow you to use an inequality operator. IComparable
and IComparable<T>
say nothing about using inequality operators directly. Instead, what they do are provide a CompareTo()
method you can use to simulate the inequality operators:
public void BubbleSort<T>(T[] array) where T: IComparable<T>
{
for (int i = 0; i < array.Length; i++)
{
for (int j = 1; j < array.Length; j++)
{
if (array[j].CompareTo(array[j-1]) < 0)
{
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With