3个回答
展开全部
using System;
namespace d{
class P{
static void Main(){
int[] ids={2,4,5,1,3};//new int[5];
string[] names={"Smith","Jhon","Mary","Cherr","Tomn"};//new string[5];
Console.WriteLine("排序前:");
Console.Write("学号:\t");
Console.WriteLine(string.Join("\t",ids));
Console.Write("姓名:\t");
Console.WriteLine(string.Join("\t",names));
int ti;string ts;
for(int i=0;i<4;i++){
for(int j=i+1;j<5;j++){
if(ids[i]>ids[j]){
ti=ids[j];ts=names[j];
ids[j]=ids[i];names[j]=names[i];
ids[i]=ti;names[i]=ts;
}
}
}
Console.WriteLine("按学号排序:");
Console.Write("学号:\t");
Console.WriteLine(string.Join("\t",ids));
Console.Write("姓名:\t");
Console.WriteLine(string.Join("\t",names));
for(int i=0;i<4;i++){
for(int j=i+1;j<5;j++){
if(names[i].CompareTo(names[j])>0){
ti=ids[j];ts=names[j];
ids[j]=ids[i];names[j]=names[i];
ids[i]=ti;names[i]=ts;
}
}
}
Console.WriteLine("按姓名排序:");
Console.Write("学号:\t");
Console.WriteLine(string.Join("\t",ids));
Console.Write("姓名:\t");
Console.WriteLine(string.Join("\t",names));
Console.ReadKey(true);
}
}
}
追问
运行后有错误
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用下.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();
}
追问
运行后有错误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询