【问题标题】:Sorting a list of items in a list box对列表框中的项目列表进行排序
【发布时间】:2011-04-09 16:29:17
【问题描述】:

我想从列表框中获取一堆项目,将它们添加到数组中,对其进行排序,然后将其放回不同的列表框中。这是我想出的:

ArrayList q = new ArrayList();
        foreach (object o in listBox4.Items)
            q.Add(o);
        q.Sort();
        listBox5.Items.Add(q.ToString());

但它不起作用。有什么想法吗?

【问题讨论】:

  • 任何原因,您不只是在第二个 ListBox 上将 ListBox.Sorted 属性设置为 true,然后只需将项目添加到 LsitBox,ListBox 就会负责排序。当然,除非您的排序标准比您的示例建议的更复杂。

标签: c# winforms listbox arraylist


【解决方案1】:

您可以只使用 ListBox.Sorted 内置功能

  foreach (object o in listBox4.Items)
  {
    listBox5.Items.Add(o);
  }
  listBox5.Sorted = true;

设置 ListBox5.Sorted=true 将确保列表框中的项目已排序,并且添加到列表框中的任何后续项目都将以正确的顺序添加。

当然,这假设您有示例所建议的简单排序要求。

【讨论】:

    【解决方案2】:
    ArrayList q = new ArrayList(); 
    foreach (object o in listBox4.Items) 
            q.Add(o);
    } 
    q.Sort(); 
    listBox5.Items.Clear();
    foreach(object o in q){
        listBox5.Items.Add(o); 
    }
    

    【讨论】:

    • 干杯老兄,帮了大忙。我怎么能用 for 循环做同样的事情?我是新手,刚刚学习
    • @Codie Vincent:你的意思是:for(int i=0;i
    【解决方案3】:

    试试这个:

    var list = lstBox.Items.Cast<ListItem>().OrderBy(item => item.Text).ToList();
    lstBox.Items.Clear();
    foreach (ListItem listItem in list)
    {
        lstBox.Items.Add(listItem);
    }
    

    如果您需要按值排序,只需将 item.Text 换成 item.Value。

    享受吧!

    【讨论】:

      【解决方案4】:

      在不向任何数组中添加元素的情况下尝试以下操作

      Listbox5.Items.AddRange(Listbox4.Items);
      Listbox5.Sorted=true;
      

      【讨论】:

        【解决方案5】:

        将项目添加到数组并关闭循环。然后对数组值进行排序并绑定到列表框

        【讨论】:

          【解决方案6】:

          尝试添加范围

              ArrayList q = new ArrayList();
          
              foreach (object o in listBox4.Items)
                  q.Add(o);
              q.Sort();
          
              listBox5.Items.AddRange(q.ToArray());
          

          【讨论】:

            【解决方案7】:

            如果您使用的是.Net3.5,请使用 linq 完成此任务。这里我使用列表进行转换和排序

                    var list = ListBox1.Items.Cast<ListItem>().Select(item => item.Value).ToList();
                    list.Sort();
            
                    ListBox2.DataSource =list;
                    ListBox2.DataBind();
            

            【讨论】:

              【解决方案8】:
                  private void SortListBox(ListBox listBox)
                  {
                      SortedList<string, string> list = new SortedList<string, string>(); 
                      foreach (ListItem i in listBox.Items) {
                              list.Add(i.Text, i.Value);
                      } 
                      listBox.Items.Clear();
                      foreach(KeyValuePair<string, string> i in list){
                          listBox.Items.Add(new ListItem(i.Key, i.Value)); 
                      }
                  }
              

              【讨论】:

                【解决方案9】:

                你也可以使用我写的“扩展方法”:

                public static class ExtensionMethods
                {
                    public static void Sort(this ListControl lb, bool desc = false)
                    {
                        var list = lb.Items.Cast<ListItem>().ToArray();
                        list = desc
                                    ? list.OrderByDescending(x => x.Text).ToArray()
                                    : list.OrderBy(x => x.Text).ToArray();
                        lb.Items.Clear();
                        lb.Items.AddRange(list);
                    }
                    public static void SortByValue(this ListControl lb, bool desc = false)
                    {
                        var list = lb.Items.Cast<ListItem>().ToArray();
                        list = desc
                                    ? list.OrderByDescending(x => x.Value).ToArray()
                                    : list.OrderBy(x => x.Value).ToArray();
                        lb.Items.Clear();
                        lb.Items.AddRange(list);
                    }
                    public static void SortByText(this ListControl lb, bool desc = false)
                    {
                        lb.Sort(desc);
                    }
                    public static void SortRandom(this ListControl lb)
                    {
                        var list = lb.Items.Cast<ListItem>()
                                            .OrderBy(x => Guid.NewGuid().ToString())
                                            .ToArray();
                        lb.Items.Clear();
                        lb.Items.AddRange(list);
                    }
                }
                

                【讨论】:

                  【解决方案10】:

                  排序列表框描述

                  void sort()
                      {
                          if (listBox1.Items.Count <= 1)
                              return;
                          for (int j = 0; j < listBox1.Items.Count - 1; j++)
                          {
                              for (int i = 0; i < listBox1.Items.Count - 1; i++)
                              {
                                  listBox1.SetSelected(i, true);
                                  string a = listBox1.SelectedItem.ToString();
                                  listBox1.SetSelected(++i, true);
                                  i--;
                                  string b = listBox1.SelectedItem.ToString();
                                  if (b.CompareTo(a) == 1)
                                  {
                                      listBox1.Items.RemoveAt(i);
                                      listBox1.Items.Insert(i, b);
                                      i++;
                                      listBox1.Items.RemoveAt(i);
                                      listBox1.Items.Insert(i, a);
                                      i--;
                                  }
                              }
                          }
                      }
                  

                  【讨论】:

                    【解决方案11】:
                    protected void Sort(ListBox lbox)
                        {
                            try
                            {
                                List<KeyValuePair<string, string>> ListBoxList = new 
                                List<KeyValuePair<string, string>>();
                                foreach (ListItem li in lbox.Items)
                                {
                                    ListBoxList.Add(new KeyValuePair<string, string>(li.Value, li.Text));
                                }
                                if (ListBoxList.Count > 0)
                                {
                                    ListBoxList = ListBoxList.OrderBy(x => x.Value).ToList();
                                    lbox.DataTextField = "Value";
                                    lbox.DataValueField = "Key";
                                    lbox.DataSource = ListBoxList;
                                    lbox.DataBind();
                                }
                            }
                            catch (Exception error)
                            {
                                error.WriteEvent();
                                throw;
                            }
                        }
                    

                    【讨论】:

                      【解决方案12】:

                      对于 ASP.NET 列表框:

                          private void SortListBox(ListBox oListBox)
                          {
                              ListBox oSortedListBox = new ListBox();
                              oSortedListBox.DataSource = oListBox.Items.Cast<ListItem>().ToDictionary(i => i.Value, i => i.Text).OrderBy(i => i.Value);
                              oSortedListBox.DataValueField = "Key";
                              oSortedListBox.DataTextField = "Value";
                              oSortedListBox.DataBind();
                      
                              oListBox.Items.Clear();
                      
                              foreach (ListItem oListItem in oSortedListBox.Items)
                              {
                                  oListBox.Items.Add(oListItem);
                              }
                          }
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2015-06-27
                        • 1970-01-01
                        相关资源
                        最近更新 更多