Pri*_*iya 0 c# arrays indexing dictionary list
我的数组字典键值对与此类似
[0]
key : x
value : 1
[1]
key : y
value : 2
[2]
key : z
value : 3
Run Code Online (Sandbox Code Playgroud)
但我需要反转字典的索引.我需要将上面的字典转换为:
[0]
key : z
value : 3
[1]
key : y
value : 2
[2]
key : x
value : 1
Run Code Online (Sandbox Code Playgroud)
我试过Reverse()函数.但它没有用.我不知道如何实现这一目标.谁能帮我这个 ?
我怎样才能做到这一点?
你不应该假设订购词典.他们不是.
如果你想要一个有序的数组,你应该使用a SortedDictionary
.如果您愿意,也可以在那里撤销订单.您应该使用自定义比较器(从此处更改):
class DescendedStringComparer : IComparer<string>
{
public int Compare(string x, string y)
{
int ascendingResult = Comparer<string>.Default.Compare(x, y);
// turn the result around
return 0 - ascendingResult;
}
}
//
SortedDictionary<string, string> test
= new SortedDictionary<string, string>(new DescendedDateComparer());
Run Code Online (Sandbox Code Playgroud)
例如,你可以迭代它foreach
.结果将按降序排序.
归档时间: |
|
查看次数: |
576 次 |
最近记录: |