C#-对列表框中的项目进行排序...! [英] C# - Sorting items in a listbox...!
本文介绍了C#-对列表框中的项目进行排序...!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我有一个包含数据的列表框:
例如
CCCCC
CCCCC
CCCCC
BBBBB
BBBBB
BBBBB
BBBBB
AAAAA
AAAAA
那么我发现使用此代码重复的项目有多少次:
Hi
I have this listbox with data:
ex.
CCCCC
CCCCC
CCCCC
BBBBB
BBBBB
BBBBB
BBBBB
AAAAA
AAAAA
then I am finding how many times are repeat items using this code:
string[] names = listBox1.Items.Cast<string>().ToArray();
List<string> namecount = (
names.GroupBy(name => name).Select(group => string.Format("{0} [{1}]", group.Key, group.Count()))
.ToList());//.ForEach(item => Console.WriteLine(item));
listBox2.DataSource = namecount;
然后列表框2如下所示:
CCCCC [3]
BBBBB [4]
AAAAA [2]
好的,现在的问题是如何将它们排序或收集到另一个列表框e.x listbox3中,如:
BBBBB [4]
CCCCC [3]
AAAAA [2]
重复最多的单词排在最前面..etc.s
and and then listbox 2 look like that:
CCCCC[3]
BBBBB[4]
AAAAA[2]
ok now the problem is how can I sort or collect them in another listbox e.x listbox3 like that:
BBBBB[4]
CCCCC[3]
AAAAA[2]
the most word that is repeat to be first.....etc
推荐答案
在构造字符串形式之前,请尝试对它们进行排序:
Try ordering them before constructing the string form:
List<string> namecount = (
names.GroupBy(name => name).OrderBy(g => g.Count()).Select(group => string.Format("{0} [{1}]", group.Key, group.Count()))
.ToList());
listBox1.Name = "listBox1";
listBox1.Items.AddRange(new object[] {
"hi",
"are",
"hello",
"how",
"wher"});
listbox1.sort = true;
这篇关于C#-对列表框中的项目进行排序...!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文