帮帮我!c#实现按字符串出现的次数排序(少到多)谢谢!!1

解决方案 »

  1.   

    比如:this is ox is ox ox
    结果:this 
         is
          ox
    就这样啊!!帮帮我!!谢谢!!
      

  2.   

            private void button2_Click(object sender, EventArgs e)
            {
                string str = "is this is ox is ox is ox";
                Hashtable ht = new Hashtable();
                string[] words = str.Split(' ');
                foreach (string word in words)
                {
                    if (ht.Contains(word))
                        ht[word] = Convert.ToInt32(ht[word]) + 1;
                    else
                        ht[word] = 1;
                }
                List<int> lstValues = new List<int>();
                List<string> lstKeys = new List<string>();
                foreach (DictionaryEntry entry in ht)
                {
                    lstValues.Add(Convert.ToInt32(entry.Value));
                    lstKeys.Add(entry.Key.ToString());
                }
                
                int[] values = lstValues.ToArray();
                string[] keys = lstKeys.ToArray();
                Array.Sort<int, string>(values, keys);            foreach (string s in keys)
                {
                    Console.WriteLine(s);
                }
            }觉得写得很烂
      

  3.   


    ArrayList str1=new ArrayList();//用来存放字符串中各字符
    ArrayList int1=new ArrayList();//用来存放各字符出现的次数
    string s=TextBox1.Text;        //用来排序的字符串
    while(s.Length!=0)
    {
    str1.Add(s.Substring(0,1));
    int i=s.Length-s.Replace(s.Substring(0,1),"").Length;
    int1.Add(i);
    s=s.Replace(s.Substring(0,1),"");
    }
    for(int i=0;i<int1.Count;i++)
    {
    for(int j=0;j<int1.Count-1;j++)
    {
    if((int)int1[j]>(int)int1[j+1])
    {
    int temp=0;
    temp=(int)int1[j];
    int1[j]=int1[j+1];
    int1[j+1]=(object)temp; string temp1="";
    temp1=(string)str1[j];
    str1[j]=str1[j+1];
    str1[j+1]=(object)temp1;
    }
    }
    }
    for(int i=0;i<str1.Count;i++)
    {
    TextBox2.Text +=(string)str1[i];  //TextBox2.Text存放排序后的字符
    }例如:在TextBox1.Text中输入“sdfdss” ,TextBox2.Text输出为:“fds”(f出现1次,d出现2次,s出现3次)
      

  4.   

    怎么又开一贴?再贴一份,呵呵using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Collections.Generic;
    using System.Collections;public class B 
    {
        static void Main()
        {
            Console.Write("输入y开始,其他键退出...");
            while ( Console.ReadKey().KeyChar == 'y')
            {
                Console.WriteLine();
                Console.WriteLine("请输入语句:");
                string input = Console.ReadLine();
                string[] items = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                Hashtable ht = new Hashtable();
                foreach (string s in items)
                {
                    ht[s] = (ht[s] == null ? 1 : Convert.ToInt32(ht[s]) + 1);
                }
                object[] keys = new ArrayList(ht.Keys).ToArray();
                object[] values = new ArrayList(ht.Values).ToArray();
                Array.Sort(values, keys);
                foreach (object o in keys) 
                {
                    Console.WriteLine(o.ToString());
                }
                Console.Write("输入Y开始,其他键退出...");
            }
        }
    }