用下.net4.0的Linq的字典排序就可以轻松搞定了
static void Main(string[] args)
{
int[] id = new int[5] { 2, 4, 5, 1, 3 };
string[] name = new string[5] { "Smith", "John", "Mary", "Cherr", "Tomn" };
Dictionary<int, string> stu = new Dictionary<int, string>();
for (int i = 0; i < id.Length; i++) stu.Add(id[i], name[i]);
Console.WriteLine("排序前:");
Console.WriteLine("学号:\t"+string.Join("\t",stu.Keys.ToArray()));
Console.WriteLine("姓名:\t"+string.Join("\t",stu.Values.ToArray()));
Console.WriteLine("按学号排序:");
Dictionary<int, string> stuid = stu.OrderBy(k => k.Key).ToDictionary(k => k.Key, p => p.Value);
Console.WriteLine("学号:\t" + string.Join("\t", stuid.Keys.ToArray()));
Console.WriteLine("姓名:\t" + string.Join("\t", stuid.Values.ToArray()));
Console.WriteLine("按姓名排序:");
Dictionary<int, string> stuname = stu.OrderBy(o => o.Value).ToDictionary(o => o.Key, p => p.Value);
Console.WriteLine("学号:\t" + string.Join("\t", stuname.Keys.ToArray()));
Console.WriteLine("姓名:\t" + string.Join("\t", stuname.Values.ToArray()));
Console.WriteLine("按任意键继续...");
Console.Read();
}
追问运行后有错误