Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends, After I sort all the data from a file, I have this kind of sort :
e.x

Cristiano Ronaldo
Cristiano Ronaldo
Cristiano Ronaldo
Van Persi
Karim Benzema
Lionel Messi
Lionel Messi
...
...

Now I need the code that this sort make like that :

Cristiano Ronaldo [3]
Van Persi [1]
Karim Benzema [1]
Lione Messi [2]


So, how many time is repeat the same name and surnme to tell in brackets []...
Posted
Comments
VJ Reddy 15-Apr-12 12:56pm    
Please see solution 3. It may be helpful to you.

Hello

For example you have a DataSet of names. Let's suppose it's a List (Simple):
C#
List<string> nameList = new List<string>();
nameList.Add("Cristiano Ronaldo");
nameList.Add("Cristiano Ronaldo");
nameList.Add("Cristiano Ronaldo");
nameList.Add("Van Persi");
nameList.Add("Karim Benzema");
nameList.Add("Lione Messi");
nameList.Add("Lione Messi");
nameList.Add("Ali Karimi");
nameList.Add("Ali Karimi");
nameList.Add("Ali Karimi");
nameList = nameList.OrderBy(n => n).ToList();


So:
C#
Dictionary<string,> nameDictionary = new Dictionary<string,>();

foreach (string s in nameList)
{
    if (nameDictionary.Keys.Contains(s))
    {
        nameDictionary[s]++;
        continue;
    }
    nameDictionary.Add(s, 1);
}

MyListBox.DataSource = new BindingSource(nameDictionary, null);


And In the Consructor of Form (For example MyForm(){...}):
C#
MyListBox.Format += new ListControlConvertEventHandler((object sender, ListControlConvertEventArgs e) =>
{
    KeyValuePair<string, int> item = (KeyValuePair<string, int>)e.ListItem;
    e.Value = string.Format("{0} [{1}]", item.Key, item.Value);
});
 
Share this answer
 
v4
Comments
h7h7h7 15-Apr-12 11:11am    
Thnx Shahin I think I understand, but you should help me now if I want the result to put in a listbox (not using console form, but windows form) .
Shahin Khorshidnia 15-Apr-12 13:41pm    
Read updated solution again.
But VJ Reddy's solution is really better.
VJ Reddy 15-Apr-12 13:44pm    
Good answer. 5!
The following code can be used to make distinct list of names along with number of occurances in the initial list.
C#
void Main()
{
    Form form1 = new Form();
    ListBox listBox1 = new ListBox();
    form1.Controls.Add(listBox1);

    List<string> names = new List<string>(){
        "Cristiano Ronaldo",  "Lione Messi",
        "Van Persi", "Karim Benzema", "Lione Messi",
        "Cristiano Ronaldo", "Cristiano Ronaldo"};
    //Make a list of sorted Distinct Names
    var distinctNames = names.Distinct().OrderBy (n => n);
    //Make list of distinctNames with count
    List<string> distinctNamesWithCount = (from name in distinctNames
        select string.Format("{0} [{1}]",name,names.Count (n => n==name ))).ToList();
    listBox1.DataSource=distinctNamesWithCount;
    form1.ShowDialog();
}
//distinctNamesWithCount contents will be

//Cristiano Ronaldo [3]
//Karim Benzema [1]
//Lione Messi [2]
//Van Persi [1]

The above code can be tested quickly using LINQPad which can be downloaded from here http://www.linqpad.net/[^]
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 15-Apr-12 13:48pm    
Good solution.
Better than mine
VJ Reddy 15-Apr-12 13:49pm    
Thank you, Shahin.
h7h7h7 15-Apr-12 17:25pm    
I can't believe, I think I have the right code but I cant implement it.
VJ Reddy I didn't need to add items in list, I already have them in a list, I just need to count those items and then to another list to put them...
So I think I don't need this code:
List<string> names = new List<string>(){
"Cristiano Ronaldo", "Lione Messi",
"Van Persi", "Karim Benzema", "Lione Messi",
"Cristiano Ronaldo", "Cristiano Ronaldo"};

But in this part how cant I take the iteme that are already in listbox...
Sory I ask a lot..
h7h7h7 15-Apr-12 17:29pm    
I can't believe, I think I have the right code but I cant implement it.
VJ Reddy I didn't need to add items in list, I already have them in a list, I just need to count those items and then to another list to put them...
So I think I don't need this code:
List<string> names = new List<string>(){
"Cristiano Ronaldo", "Lione Messi",
"Van Persi", "Karim Benzema", "Lione Messi",
"Cristiano Ronaldo", "Cristiano Ronaldo"};

But in this part how cant read items in a listbox that are already and then to
Sory I ask a lot.... have the result e.x
//Cristiano Ronaldo [3]
//Karim Benzema [1]
//Lione Messi [2]
//Van Persi [1]
VJ Reddy 15-Apr-12 20:15pm    
If you already have list of names, then use that list instead of List names = ...
If the names are there in a list box then
var names = listBoxNames.Items;
Once, you obtain the names list, then the rest of code will remain as it is.
In case of any problem, please post your code where names are there.
Solution 3 is good :)

You might try this one as well,

C#
using System;
using System.Collections.Generic;
using System.Linq;

namespace Chapter_5
{
    class Program
    {
        static void Main(string[] args)
        {

            List<string> names = new List<string>()
            {
                "Cristiano Ronaldo",  "Lione Messi",
                "Van Persi", "Karim Benzema", "Lione Messi",
                "Cristiano Ronaldo", "Cristiano Ronaldo",
            };
            names.GroupBy(name => name).Select(group => string.Format("{0} [{1}]", group.Key, group.Count()))
                .ToList().ForEach(item => Console.WriteLine(item));
        }
    }
}</string></string>


Hope it helps :)
 
Share this answer
 
Comments
VJ Reddy 15-Apr-12 23:27pm    
Good solution. 5!
Mohammad A Rahman 15-Apr-12 23:33pm    
Thanks VJ :)
h7h7h7 16-Apr-12 3:56am    
It works, thanks to all,...I have done a combination of code from VJ and Mohammad...thnx thnx GOD Bless you !

Thnx Shahin !
Mohammad A Rahman 16-Apr-12 6:02am    
Not a problem :)
Create some sort of List(T)[^], or other generic type and count the occurrences of each name before printing.
 
Share this answer
 
Comments
h7h7h7 15-Apr-12 10:27am    
Richard, I am not so good in c#, I need a code in windows form...
So based in the case above can write a simple code here about that problem !
[no name] 15-Apr-12 10:34am    
What exactly have you tried?
h7h7h7 15-Apr-12 10:52am    
I can get a data from a file, those data I put in list box and the result is ex:
AAA
AAA
AAA
BBB
CCC
CCC
and now I want to count them and put to another textbox or listbox like that:
AAA [3] .... "is repeat three times"
BBB [1] ... "is repeat one time "
CCC [2] ... "is repat two time"
CCC [1]
[no name] 15-Apr-12 10:56am    
So, in other words, you have not actually tried to do anything except try and get someone else to write code for you. Writing code for you is not going to teach me anything and you sure won't learn anything.
h7h7h7 15-Apr-12 11:02am    
I know it Wes Aday you are right, But I am in that situation that I need to do it quickly...when I have more time, I will try everything to do alone without others help. THnx

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