如何按值列表整理此列表的字典词典?

问题描述:

你好,我有一个问题,整数这个字典的int值。如何按值列表整理此列表的字典词典?

这是字典:

_cleanHelper = 
    new Dictionary<Direction, Dictionary<CleanTypes, List<List<int>>>>() 
{ 
    { 
     Direction.North, new Dictionary<CleanTypes, List<List<int>>>() 
     { 
      { 
       CleanTypes.Walled, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         0, -1 
        }, 
        new List<int>() 
        { 
         1, -1 
        }, 
        new List<int>() 
        { 
         1, 0 
        }, 
        new List<int>() 
        { 
         1, 1 
        }, 
        new List<int>() 
        { 
         0, 1 
        } 
       } 
      }, 
      { 
       CleanTypes.Close, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         0, 0 
        } 
       } 
      }, 
      { 
       CleanTypes.Recurse, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         -1, 0 
        } 
       } 
      } 
     } 
    }, 
    { 
     Direction.South, new Dictionary<CleanTypes, List<List<int>>>() 
     { 
      { 
       CleanTypes.Walled, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         0, -1 
        }, 
        new List<int>() 
        { 
         -1, -1 
        }, 
        new List<int>() 
        { 
         -1, 0 
        }, 
        new List<int>() 
        { 
         -1, 1 
        }, 
        new List<int>() 
        { 
         0, 1 
        } 
       } 
      }, 
      { 
       CleanTypes.Close, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         0, 0 
        } 
       } 
      }, 
      { 
       CleanTypes.Recurse, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         1, 0 
        } 
       } 
      } 
     } 
    }, 
    { 
     Direction.West, new Dictionary<CleanTypes, List<List<int>>>() 
     { 
      { 
       CleanTypes.Walled, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         -1, 0 
        }, 
        new List<int>() 
        { 
         -1, 1 
        }, 
        new List<int>() 
        { 
         0, 1 
        }, 
        new List<int>() 
        { 
         1, 1 
        }, 
        new List<int>() 
        { 
         1, 0 
        } 
       } 
      }, 
      { 
       CleanTypes.Close, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         0, 0 
        } 
       } 
      }, 
      { 
       CleanTypes.Recurse, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         0, -1 
        } 
       } 
      } 
     } 
    }, 
    { 
     Direction.East, new Dictionary<CleanTypes, List<List<int>>>() 
     { 
      { 
       CleanTypes.Walled, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         -1, 0 
        }, 
        new List<int>() 
        { 
         -1, -1 
        }, 
        new List<int>() 
        { 
         0, -1 
        }, 
        new List<int>() 
        { 
         1, -1 
        }, 
        new List<int>() 
        { 
         1, 0 
        } 
       } 
      }, 
      { 
       CleanTypes.Close, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         0, 0 
        } 
       } 
      }, 
      { 
       CleanTypes.Recurse, new List<List<int>>() 
       { 
        new List<int>() 
        { 
         0, 1 
        } 
       } 
      } 
     } 
    } 
}; 

在运行时我做的这几个副本,需要有一种方法由按升序排列整数值对它们进行排序。我最好怎么做?

谢谢你的帮助!

+0

一个词典列表的整数列表词典?圣母玛利亚。难道你不能用某种'Vector'或'Point'数据类型来清理它吗? – JosephHirn 2013-02-11 14:20:30

+1

重新设计您的数据结构可能是明智的... – Syjin 2013-02-11 14:22:13

我想你想改变包含的值。这意味着这可能是你追求的:

foreach (var item in _cleanHelper) 
{ 
    foreach (var jtem in item.Value) 
    { 
     jtem.Value.Sort((a, b) => 
     { 
      int cmp = a[0].CompareTo(b[0]); 
      return (cmp == 0) ? a[1].CompareTo(b[1]) : cmp; 
     }); 
    } 
} 

这么说,我从来没有实现它是这样的...我可能会创建一个坐标的“点”类,而在看你可能会把它们作为单个'方向'类或类似的东西来实现。这可能会使您的代码更具可读性,并且作为奖励使结构更易于用f.ex来遍历。 LINQ。

+0

请问downvoter能解释一下吗? – atlaste 2013-04-05 17:49:26