0

I have such a problem.
I sorted all the numbers in the list and printed them in the listBox. I read the numbers from the txt file which I make an array.
I need the user to type in any number (which I kept in the variable "a") those numbers in the following order:

  • first the numbers less than a
  • then the numbers equal to a
  • and finally the big numbers.

and print it all in listBox.

...
float x;
if (float.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out x))
{
    lst.Items.Add(x);
}
List<float> array = new List<float>();
array.Add(x);
a = Convert.ToInt32(txt1.Text);
int at = lst2.Items.Count;
for (int o = 0; o < lst2.Items.Count; ++o)
{
    if (x < (float)(lst2.Items[o]) && a >= o)
    {
        at = o;
        break;
    }
}
lst2.Items.Insert(at, x);

With this code I only sort the numbers without sorting with a variable.

djv
  • 15,168
  • 7
  • 48
  • 72
Artur
  • 53
  • 5
  • 1
    `first the numbers less than a, then the numbers equal to a, and finally the big numbers` - sounds like an order that you would get without involving any `a` variable. – GSerg Dec 20 '21 at 16:36
  • @GSerg Probably "numbers less than a" and "the big numbers" can be unsorted. – Dmitry Dec 20 '21 at 16:41
  • 1
    @Dmitry Given the `I sorted all the numbers in the list`, it is the same order. – GSerg Dec 20 '21 at 16:44
  • No No. I understand the second part of the problem seems pointless, but all the numbers will actually be sorted, and I get it (the numbers are arranged in order from smallest to largest), but the value entered by the user (which will be just 1 number) is also needed. be included in that sorting – Artur Dec 20 '21 at 16:48
  • 1
    Does this answer your question? [How to insert item into list in order?](https://stackoverflow.com/questions/12172162/how-to-insert-item-into-list-in-order) – GSerg Dec 20 '21 at 16:50

1 Answers1

0

You can just get the numbers which are less than, equal to, and greater than a, then populate the ListBoxes.

private List<float> numbers = new List<float>();

private void Form1_Load(object sender, EventArgs e)
{
    numbers = File.ReadAllLines("input.txt").Select(s => float.Parse(s)).ToList();
}

private void button1_Click(object sender, EventArgs e)
{
    if (float.TryParse(txt1.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out float a))
    {
        // add a to numbers?
        numbers.Add(a);
        var lessThan = numbers.Where(n => n < a);
        var equalTo = numbers.Where(n => n == a);
        var greaterThan = numbers.Where(n => n > a);
        // populate listboxes?
        lst.DataSource = lessThan.ToList();
        lst1.DataSource = equalTo.ToList();
        lst2.DataSource = greaterThan.ToList();
    }
}

input.txt

123.45
1.2345
0.12345
12345
12.345
1234.5

enter image description here

If you want them all sorted, you can OrderBy when reading from the file

numbers = File.ReadAllLines("input.txt").Select(s => float.Parse(s)).OrderBy(n => n).ToList();
djv
  • 15,168
  • 7
  • 48
  • 72