日期:2014-05-18  浏览次数:20797 次

C#统计后排序
有一个字符串string,3242615284293,把每个数出现的次数统计然后按出现次数排列出来,显示到textbox中。在线等,谢谢大家了。

------解决方案--------------------
string s = "3242615284293";
var query = from x in s group x by x into g select new { g.Key, Count = g.Count() } orderby Count;
textBox1.Text = string.Join(";", query.Select(x => x.Key + ":" + x.Count));
------解决方案--------------------
C# code
            string s = "3242615284293";
            int[] times = new int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            foreach (char c in s)
                times[int.Parse(c.ToString())]++;
            string r = "";
            for (int i = 0; i < times.Length; i++)
                r += (string.Format("[{0}]:{1}; ", i, times[i]));
            textBox1.Text = r;

------解决方案--------------------
C# code
string str = "3242615284293";
textBox1.Text = new string(str.GroupBy(v1 => v1).OrderBy(v1 => v1.Count()).Reverse().Select(v1 => v1.Key).ToArray());
//24398516