Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
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

C#
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
Posted
Updated 23-May-12 4:34am
v2
Comments
Ganesan Senthilvel 23-May-12 10:34am    
Code format is done
R. Giskard Reventlov 23-May-12 11:27am    
Why don't you just sort the data before it gets anywhere near the listbox? How are you getting the data? From a table?
Sergey Alexandrovich Kryukov 23-May-12 11:40am    
Exactly.
--SA
Chris Ross 2 23-May-12 11:47am    
Unfortunately the OP cannot sort on the source data because it is just the name/country text - without the appended '[nnn]'. And he wants to sort on the 'nnn'.
R. Giskard Reventlov 23-May-12 12:44pm    
He could add a column to the table with a number in for sorting then order by that number, for instance. select stuff from table order by new_sort_column

Hi ,
Check this i believe this what you want
C#
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;
          }
      }

Best Regards
M.Mitwalli
 
Share this answer
 
v2
Comments
Mohamed Mitwalli 23-May-12 14:49pm    
This is regarding your old post
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.
 
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