您好我有一点问题是按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 :(得分:-1)
我想你想要改变包含的值。这意味着这可能就是你所追求的:
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;
});
}
}
那就是说,我从来没有像这样实现它...我可能会为坐标创建一个'Point'类,并查看用于字典的键,我可能会将它们作为单个实现'方向'类或类似的东西。这可能会使您的代码更具可读性,并且作为奖励使得结构更容易遍历f.ex. LINQ。