根据字符</point,>对字典<point,int =“”>进行排序

时间:2014-05-31 22:30:35

标签: c# linq sorting dictionary

我有一个Dictionary<Point, int> MyDicPoint类的定义如下:

public class Point
{
    public double X { get; set; }
    public double Y { get; set; }
}

如何使用MyDic根据KeyLINQ进行排序?我想按X然后按Y订购。

例如,如果我的字典如下所示:

Key (Point (X,Y))          Value (int)
--------------------------------------
(8,9)                      6
(5,4)                      3
(1,4)                      2
(11,14)                    1

排序后会是这样的:

Key (Point (X,Y))          Value (int)
--------------------------------------
(1,4)                      2
(5,4)                      3
(8,9)                      6
(11,14)                    1

1 个答案:

答案 0 :(得分:3)

OrderByThenBy应该为您解决问题:

MyDic.OrderBy(x => x.Key.X)
    .ThenBy(x => x.Key.Y)
    .ToDictionary(x => x.Key, x => x.Value)
相关问题