Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have items in list/ I want see top 10 frequency data in list/ How make it
Posted
Updated 13-Aug-12 1:47am
v2

Why dont you look at list.Sort method:
http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx[^]
 
Share this answer
 
Comments
[no name] 13-Aug-12 7:53am    
Sort is not help me to make frequecy of data/ Example#

table
chair
hat
chair
table
table

i want sort it by frequency
table
chair
hat
Not a very nice solution, but it does what you want it to do with your given example:
C#
private void Form1_Load(object sender, EventArgs e)
{
   List<string> lst = new List<string>() { "table", "chair", "hat", "chair", "table", "table", };

   Dictionary<string, int> frequencies = new Dictionary<string, int>();
   foreach (string item in lst)
   {
      if (frequencies.ContainsKey(item))
         frequencies[item]++;
      else
         frequencies.Add(item, 1);
   }

   //sort the dictionary
   var sortedDict = (from entry in frequencies orderby entry.Value descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
   lst.Clear();

   //add the items back to the original list
   foreach (string entry in sortedDict.Keys)
      lst.Add(entry);

   //output the sorted list
   foreach(string val in lst)
      Console.WriteLine(val);

   //output the sorted dictionary
   foreach (KeyValuePair<string, int> pair in sortedDict)
      Console.WriteLine(pair.Key + " " + pair.Value.ToString() + "pc");
}
 
Share this answer
 
v3
Comments
[no name] 13-Aug-12 8:20am    
can you make

table 3pc
chair 2pc
hat 1pc
JF2015 14-Aug-12 1:30am    
I updated the code to display the output as you wanted it to be and also fixed a small issue regarding the sorting.

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