-1

您好,我在按 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
                    }
                }
            }
        }
    }
};

在运行时,我制作了几个副本,并且需要有一种方法按整数值升序对它们进行排序。我将如何最好地做到这一点?

谢谢您的帮助!

4

1 回答 1

-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;
        });
    }
}

也就是说,我永远不会像这样实现它......我可能会为坐标创建一个“点”类,并查看您用于字典的键,我可能会将它们实现为单个“方向”类或类似的东西。这可能会使您的代码更具可读性,并且作为奖励使结构更易于使用 f.ex 遍历。林克。

于 2013-02-11T14:14:00.627 回答