110,534
社区成员
发帖
与我相关
我的任务
分享
int a = 3;
int b = 5;
int c = 9;
int d = 1;
int e = 7;
Dictionary<int, string> dict1= new Dictionary<int,string>
{
{a, "OK"}
};
Dictionary<int, string> dict2= new Dictionary<int,string>
{
{b, "OKK"}
};
Dictionary<int, string> dict3= new Dictionary<int,string>
{
{c, "OOKK"}
};
Dictionary<int, string> dict4= new Dictionary<int,string>
{
{d, "OKOK"}
};
Dictionary<int, string> dict5= new Dictionary<int,string>
{
{e, "OKKO"}
};
Dictionary<int, string>[] dict = new Dictionary<int,string>[]
{
dict1, dict2, dict3, dict4, dict5
};
Console.WriteLine(" {0} {1} {2} {3} {4}", dict[0].Values.First(), dict[1].Values.First(), dict[2].Values.First(), dict[3].Values.First(), dict[4].Values.First());
我要打印出来的value(string), 是按照key(int)排列好的顺序打印的dict.OrderBy(x=>x.Keys.First())
Console.WriteLine(" {0} {1} {2} {3} {4}", dict[0].Values.First(), dict[1].Values.First(), dict[2].Values.First(), dict[3].Values.First(), dict[4].Values.First());
我要打印出来的value(string), 是按照key(int)排列好的顺序打印的[/quote]
你需要用变量引用的,OrderBy 他返回的对象是排序好的,并非对原集合进行修改。
int a = 3;
int b = 5;
int c = 9;
int d = 1;
int e = 7;
Dictionary<int, string> dict1 = new Dictionary<int, string> { { a, "OK" } };
Dictionary<int, string> dict2 = new Dictionary<int, string> { { b, "OKK" } };
Dictionary<int, string> dict3 = new Dictionary<int, string> { { c, "OOKK" } };
Dictionary<int, string> dict4 = new Dictionary<int, string> { { d, "OKOK" } };
Dictionary<int, string> dict5 = new Dictionary<int, string> { { e, "OKKO" } };
Dictionary<int, string>[] dict = new Dictionary<int, string>[] { dict1, dict2, dict3, dict4, dict5 };
var orderedDict = dict.OrderBy(subDict => subDict.Keys.First());
foreach (var item in orderedDict)
{
Console.WriteLine(item.First());
}
你可以再试试![/quote]
谢谢,这样是可以遍历打印出来,但是我想要排序后单独拿出来使用,像 dict[0].Values.First(),怎么能拿到这个sring?
谢谢![/quote]
var specifiedItem = orderedDict.ElementAt(1).First();
Console.WriteLine(specifiedItem.Key + " : " + specifiedItem.Value);
Console.WriteLine(" {0} {1} {2} {3} {4}", dict[0].Values.First(), dict[1].Values.First(), dict[2].Values.First(), dict[3].Values.First(), dict[4].Values.First());
我要打印出来的value(string), 是按照key(int)排列好的顺序打印的[/quote]
你需要用变量引用的,OrderBy 他返回的对象是排序好的,并非对原集合进行修改。
int a = 3;
int b = 5;
int c = 9;
int d = 1;
int e = 7;
Dictionary<int, string> dict1 = new Dictionary<int, string> { { a, "OK" } };
Dictionary<int, string> dict2 = new Dictionary<int, string> { { b, "OKK" } };
Dictionary<int, string> dict3 = new Dictionary<int, string> { { c, "OOKK" } };
Dictionary<int, string> dict4 = new Dictionary<int, string> { { d, "OKOK" } };
Dictionary<int, string> dict5 = new Dictionary<int, string> { { e, "OKKO" } };
Dictionary<int, string>[] dict = new Dictionary<int, string>[] { dict1, dict2, dict3, dict4, dict5 };
var orderedDict = dict.OrderBy(subDict => subDict.Keys.First());
foreach (var item in orderedDict)
{
Console.WriteLine(item.First());
}
你可以再试试![/quote]
谢谢,这样是可以遍历打印出来,但是我想要排序后单独拿出来使用,像 dict[0].Values.First(),怎么能拿到这个sring?
谢谢!
Console.WriteLine(" {0} {1} {2} {3} {4}", dict[0].Values.First(), dict[1].Values.First(), dict[2].Values.First(), dict[3].Values.First(), dict[4].Values.First());
我要打印出来的value(string), 是按照key(int)排列好的顺序打印的[/quote]
你需要用变量引用的,OrderBy 他返回的对象是排序好的,并非对原集合进行修改。
int a = 3;
int b = 5;
int c = 9;
int d = 1;
int e = 7;
Dictionary<int, string> dict1 = new Dictionary<int, string> { { a, "OK" } };
Dictionary<int, string> dict2 = new Dictionary<int, string> { { b, "OKK" } };
Dictionary<int, string> dict3 = new Dictionary<int, string> { { c, "OOKK" } };
Dictionary<int, string> dict4 = new Dictionary<int, string> { { d, "OKOK" } };
Dictionary<int, string> dict5 = new Dictionary<int, string> { { e, "OKKO" } };
Dictionary<int, string>[] dict = new Dictionary<int, string>[] { dict1, dict2, dict3, dict4, dict5 };
var orderedDict = dict.OrderBy(subDict => subDict.Keys.First());
foreach (var item in orderedDict)
{
Console.WriteLine(item.First());
}
你可以再试试!