C# 辞書のソート (Key 値と Value 値による逆順のソート)

辞書の並べ替えは、簡単なクエリのためにメモを作成するだけです (System.Linq; を使用して名前空間を参照します)。

Dictionary<int, int> tempDict = new Dictionary<int, int>();
var sortResult1 = from pair in tempDict orderby pair.Value descending select pair; //以字典Value值逆序排序
var sortResult2 = from pair in tempDict orderby pair.Key descending select pair; //以字典Key值逆序排序
var sortResult3 = from pair in tempDict orderby pair.Key ascending select pair; //以字典Key值顺序排序
var sortResult4 = from pair in tempDict orderby pair.Value ascending select pair; //以字典Value值顺序排序

要約:

  1. 取得されたソート構造 sortResult1,2,3,4 はイテレータIOrderedEnumerable<>であり、辞書ではないことに注意してください。
Dictionary<int, int> sortResult1 = from pair in tempDict orderby pair.Value descending select pair; //以字典Value值逆序排序
  1. ディクショナリのキーまたは値が参照型の場合、参照型のフィールドに従って並べ替えることができます。
public class Info
{
    
    
	public int m_ID;
}
Dictionary<int, Info> tempDict = new Dictionary<int, Info>();
var sortResult1 = from pair in tempDict orderby pair.Value.m_ID descending select pair; //以字典Value值的字段 m_ID 逆序排序

おすすめ

転載: blog.csdn.net/weixin_42205218/article/details/105934030
おすすめ