对包含dictionary的对象列表进行排序



这是我的对象:

public class A
{
public string Name { get; set; }    
public Dictionary<int, List<int>> Years { get; set; }
}

字典包含年份列表,月份列表。我希望按年排序 A列表,然后按月排序。

每个A的字典样本:

{2015,new List(){3,4,5,6}}
{2016,new List(){2,8,9,10}}

如果我有多年,那么它可以排序如下:2015然后2015,2016然后2016

我未能实现IComparable或使用任何现有的扩展方法,如OrderBy或Sort。谢谢!

我将IComparable添加到您的A类(当然您可以提取并作为委托传递.....)

fiddlerlink

注:为简单起见,我假设列表是排序的。否则,在场景中的适当位置添加一个您的排序:)

基本上返回第一个不等于年或月的比较值。你可以通过MyListOfAs.Sort();

public class A:IComparable<A>
{
    public string Name { get; set; }
    public Dictionary<int, List<int>> Years { get; set; }
    private int complists(List<int> a, List<int> b)
    {
        var iret = (from i in Enumerable.Range(0, Math.Min(a.Count, b.Count))
                     where a[i].CompareTo(b[i]) != 0
                     select a[i] > b[i] ? 1 : -1).FirstOrDefault();
        return iret;
    }
    public int CompareTo(A other)
    {
        var mykeys = this.Years.Keys.ToList();
        var otherkeys = other.Years.Keys.ToList();
        var iret = (from i in Enumerable.Range(0, Math.Min(mykeys.Count, otherkeys.Count))
                     let yearDiff = mykeys[i].CompareTo(otherkeys[i])
                     let monthDiff = complists(this.Years[mykeys[0]], other.Years[otherkeys[0]])
                     where yearDiff != 0 || monthDiff != 0
                     select yearDiff != 0 ? yearDiff : monthDiff).FirstOrDefault();
        return iret != 0 ? iret : mykeys.Count > otherkeys.Count ? 1 : mykeys.Count < otherkeys.Count ? -1 : 0;
    }    }

我想这应该可以做到:listOfA.OrderBy(a => a.Years.Min(y => y.Key + y.Value.Min() / 13d))

例如,如果字典中最低的年份和月份是2000 Jan,那么它的值将是2000 + 1/13 = 2000.0769

那么剩下的就是从每个字典中选择最小的值,并按此排序。

注意13也可以是20,如果您出于某种原因想要更好的数字,但是12/x的最大分数必须小于1,以便适当地给予年份更多的重要性,因此x必须大于12。这里假设月份的范围为1到12。


I/O例子:

var listOfA = new List<A>
              {
                  new A
                  {
                      Name = "a1",
                      Years = new Dictionary<int, List<int>>
                              {
                                  {2015, new List<int> {3, 4, 5, 6}},
                                  {2016, new List<int> {2, 8, 9, 10}}
                              }
                  },
                  new A
                  {
                      Name = "a2",
                      Years = new Dictionary<int, List<int>>
                              {
                                  {2013, new List<int> {3, 4, 5, 6}},
                                  {2014, new List<int> {2, 8, 9, 10}}
                              }
                  },
                  new A
                  {
                      Name = "a3",
                      Years = new Dictionary<int, List<int>>
                              {
                                  {2015, new List<int> {3, 4, 5, 6}},
                                  {2014, new List<int> {2, 8, 9, 10}}
                              }
                  },
                  new A
                  {
                      Name = "a4",
                      Years = new Dictionary<int, List<int>>
                              {
                                  {2014, new List<int> {1, 4, 5, 6}},
                                  {2017, new List<int> {2, 8, 9, 10}}
                              }
                  }
              };
// listOfA is now {a1, a2, a3, a4}
listOfA = listOfA.OrderBy(a => a.Years.Min(y => y.Key + y.Value.Min() / 13d)).ToList();
// listOfA is now {a2, a4, a3, a1}

相关内容

  • 没有找到相关文章

最新更新