Can anybody tell me how can i sort elements of an integer array in descending order? is it possible?
thank you
Can anybody tell me how can i sort elements of an integer array in descending order? is it possible?
thank you
Use Sort() followed by Reverse()
Sort() followed by Reverse() will execute almost three times as fast as calling Sort with a comparison delegate. See informit.com/guides/content.aspx?g=dotnet&seqNum=781 for the reasons why.This may help if you have a class assignment and your professor doesn't want you to use Array.Sort.
private void button1_Click(object sender, EventArgs e)
{
int[] numbers = { 7, 4, 2, 6, 1, 10, 5, 8, 9, 3 };
SortArray(numbers);
foreach (int n in numbers)
{
textBox1.Text = textBox1.Text + n.ToString() + ' ';
}
}
public static void SortArray(int[] num)
{
int temp;
int count = 0;
while (count < num.Length - 1)
{
int min = num[count];
for (int x = count; x < num.Length; x++)
{
if (min > num[x])
{
temp = num[count];
min = num[x];
num[count] = num[x];
num[x] = temp;
}
}
count++;
}
}