【问题标题】:Sort float numbers List from small to large [closed]从小到大对浮点数列表进行排序[关闭]
【发布时间】:2022-01-16 20:57:05
【问题描述】:

我想对浮点数列表从小到大进行排序。我尝试使用 array.Sort () 方法,但它不起作用。 下面是我的代码。

 List<float> array = new List<float>();
      array.Add(x);
      listBox1.Items.Add(x);

【问题讨论】:

  • 您的代码没有显示List&lt;T&gt;.Sort 方法的使用。仅供参考,将列表变量命名为 array 有点奇怪。
  • 大胆猜测:您是否告诉您的列表框按字母顺序排序?
  • @KlausGütter 不,我没有。我该怎么做?
  • 我不知道您的列表框是 Winforms、WPF、UWP、Xamarin 还是...
  • @KlausGütter 这是 Winforms

标签: c# windows


【解决方案1】:

只要Sort():

List<float> array = new List<float>();

array.Add(x);

array.Sort();

当array(List&lt;float&gt; 的一个奇怪名称)被排序后,我们可以插入x在listBox1.Items中间的某个位置。假设listBox1.Items 只包含有序的float 项,我们可以放

// 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);

【讨论】:

  • 如果你有一个排序列表,你可以简单地将它分配给列表框的数据源:listBox1.DataSource = array;
  • @OlivierJacot-Descombes 好吧,我也会尝试这个选项。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多