在列表框中排序有麻烦....! [英] sorting in List box trouble....!

查看:86
本文介绍了在列表框中排序有麻烦....!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样对列表框中的数据进行排序:
梅苏厄齐尔德国[5]
阿根廷梅西[4]
葡萄牙ronado [3]

因此,括号中的最高值应排在第一位:
我正在使用此代码,但未进行排序

I want to sort those data in a listbox like that:
mesu ozil germany [5]
mesi argentina [4]
ronado portugal [3]

so the high value in bracket to be first :
I am using this code but they are not sorting

String[] sort= listBox1.Items.Cast().ToArray();
            List count= (
             sort.GroupBy(name => name).OrderByDescending(g => g.Count())
             .Select(group => string.Format("{0} [{1}]", group.Key, group.Count())).ToList());
            listBox2.DataSource = count

推荐答案


检查一下,我相信这是您想要的
Hi ,
Check this i believe this what you want
private void button1_Click(object sender, EventArgs e)
      {

          OpenFileDialog dialogu = new OpenFileDialog();
          dialogu.Filter = "*.txt|*.txt";
          if (dialogu.ShowDialog() == DialogResult.OK)
          {
              string[] all = File.ReadAllLines(dialogu.FileName, System.Text.Encoding.Default);
              listBox1.DataSource = all;
              all.ToList();

              Dictionary<string,int> dict = new Dictionary<string,int>();
              foreach (string item in all)
              {
                    int len = item.IndexOf("[");
                  int len2 = item.IndexOf("]");
                  string itm = item.Remove(0, len+1); ;

                  string key1 = item.Substring(0, len);
                  string value1 = itm.Substring(0, itm.Length-1);

                  dict.Add(key1,Convert.ToInt32( value1));
              }
              var res = (from x in dict
                        orderby x.Value descending
                        select x).ToList();
              listBox1.DataSource = null;
              listBox1.DataSource = res;
          }
      }


最好的问候
M.Mitwalli


Best Regards
M.Mitwalli


我使用您的Linq表达式进行了测试-并且它(Linq表达式)工作了.但是我希望你已经知道了.

也许您已将listBox2的Sorted属性设置为true?这将撤消您在Linq表达式中所做的任何排序,因为它将根据字符串的名称部分而不是计数部分对字符串进行重新排序.
I did a test - using your Linq expression - and it (the Linq expression) worked. But I expect you knew that already.

Perhaps you''ve set the Sorted property of listBox2 to true? That would undo any sorting you did in your Linq expression because it would re-sort the strings based on the name portion of the strings, not on the count part.


这篇关于在列表框中排序有麻烦....!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆