I am trying to manually sort an array using string inputs that end with a number which I want to sort from highest to lowest.
For example I can start with this output: ,Name1: 1540 ,Name2: 2660 ,Name3: 80 ,Name4: 380
And in the end it should look like this: ,Name2: 2660 ,Name1: 1540 ,Name4: 380 ,Name3: 80
private string[] OrderHighToLow(string[] data)
{
string temp;
for (int i = 0; i < data.Length; i++)
{
for (int y = 0; y < i; y++)
{
if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) > int.Parse(data[i].Substring((data[i].IndexOf(':') + 2))))
{
temp = data[i];
data[i] = data[y];
data[y] = temp;
}
}
}
return data;
}
This is what I have tested. According to me, this should work, but the point is it doesn't, the application just crashes. So, if anyone here can figure out why that may be, I would be very thankful. Thanks in advance.
datadoesn't look how you think it does. Put a breakpoint on the entry to your function and inspect that data. Most likely with the way you're parsing it, you're ending up with an empty string or something.