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); for(int i=keys.Length-1;i>=0;i--) { Console.WriteLine(keys[i].ToString()); } Console.Write("输入Y开始,其他键退出..."); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsApplication6 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public class DinoComparer : IComparer<Return> { public int Compare(Return x, Return y) { if (x == null) { if (y == null) { // If x is null and y is null, they're // equal. return 0; } else { // If x is null and y is not null, y // is greater. return -1; } } else { // If x is not null... // if (y == null) // ...and y is null, x is greater. { return 1; } else { return x.Count.CompareTo(y.Count); } } } } public class Return { public string Value; public int Count; } private void button1_Click(object sender, EventArgs e) { string bs1 = "this is apple apple this this"; string[] sa1 = bs1.Split(new char[] { ' '}); Dictionary<string, Return> test = new Dictionary<string, Return>(); foreach (string item in sa1) { if (!test.ContainsKey(item)) { Return Return = new Return(); Return.Value = item; Return.Count = 0; test.Add(item, Return); } else { test[item].Count++; } } List<Return> list = new List<Return>(); list.AddRange(test.Values); DinoComparer dc = new DinoComparer(); list.Sort(dc); List<string> Out = new List<string>(); foreach (Return Return in list) { Out.Add(Return.Value); } } } }
不好意思,我在二楼贴的代码刚好相反,从多到少列出,改了一下,重贴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开始,其他键退出..."); } } }
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);
for(int i=keys.Length-1;i>=0;i--)
{
Console.WriteLine(keys[i].ToString());
}
Console.Write("输入Y开始,其他键退出...");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public class DinoComparer : IComparer<Return>
{
public int Compare(Return x, Return y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{ return x.Count.CompareTo(y.Count); }
}
}
} public class Return
{
public string Value;
public int Count;
} private void button1_Click(object sender, EventArgs e)
{
string bs1 = "this is apple apple this this";
string[] sa1 = bs1.Split(new char[] { ' '});
Dictionary<string, Return> test = new Dictionary<string, Return>();
foreach (string item in sa1)
{
if (!test.ContainsKey(item))
{
Return Return = new Return();
Return.Value = item;
Return.Count = 0; test.Add(item, Return);
}
else
{
test[item].Count++;
}
}
List<Return> list = new List<Return>();
list.AddRange(test.Values);
DinoComparer dc = new DinoComparer();
list.Sort(dc); List<string> Out = new List<string>(); foreach (Return Return in list)
{
Out.Add(Return.Value);
} }
}
}
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开始,其他键退出...");
}
}
}