I want to sort float numbers List from small to large. I tried to use the array.Sort () method but it doesn’t work.
Below is my code.
List<float> array = new List<float>();
array.Add(x);
listBox1.Items.Add(x);
>Solution :
Just Sort():
List<float> array = new List<float>();
array.Add(x);
array.Sort();
When array (quite a strange name for List<float>) is sorted, we can insert x somewhere in the middle of listBox1.Items. Asuming that listBox1.Items contains ordered float items only, we can put
// Here's we have WinForms ListBox
int at = listBox1.Items.Count;
for (int i = 0; i < listBox1.Items.Count; ++i) {
if (x < (float) (listBox1.Items[i])) {
at = i;
break;
}
}
listBox1.Items.Insert(at, x);