Click here to Skip to main content
15,904,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
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;


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
Posted

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());
 
Share this answer
 
Comments
h7h7h7 8-May-12 8:05am    
thnx it work but they are grouping from small value to high value like that:
AAA[1]
BBB[2]
CCC[3]

what should I change in code to group from high value to small value like that:
CCC[3]
BBB[2]
AAA[1]


thankyou so much
h7h7h7 8-May-12 8:21am    
it is ok man, I solved it using orderByDescending ....

thnx a lot
C#
listBox1.Name = "listBox1";
listBox1.Items.AddRange(new object[] {
             "hi",
            "are",
            "hello",
            "how",
            "wher"});
listbox1.sort = true;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900