c# hashtable排序问题
以下是我编的程序,主要功能,是可以输入一句话,然后统计这段话中每个单词出现的次数,然后出现次数少的在前,多的在后,就是说按升序排列,应该是对HashTable的VALUE排序,不过我弄不出来,以下是我的代码!我是用VS 2005进行编辑的!
using System;
using System.Collections;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Please enter one sentence");
String strSentence = Console.ReadLine();
String[] strArr = strSentence.Split();
Hashtable hTable = new Hashtable();
for (int i = 0; i < strArr.Length; i++)
{
String strCurrWord = strArr[i];
if (hTable.ContainsKey(strCurrWord))
{
int counter = (int)hTable[strCurrWord];
counter++;
hTable[strCurrWord] = counter;
}
else
{
hTable[strCurrWord] = 1;
}
}
ArrayList newArrayList = new ArrayList(hTable.Values);
newArrayList.Sort(); // 升序排列
ArrayList newArray = new ArrayList();
foreach (int score in newArrayList)
{
if (!newArray.Contains(score))
{
ICollection colKeys = hTable.Keys;
IEnumerator it = colKeys.GetEnumerator();
while (it.MoveNext())
{
if ((int)it.Current== score)
{
String strKey = (String)it.Current;
Console.Out.WriteLine("The word" + strKey + "has appeared" + hTable[strKey] + "times!");
}
}
newArray.Add(score);
}
}
Console.ReadLine();
}
}
}