Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort float numbers List from small to large

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading