自定义字符顺序

时间:2015-03-25 01:08:40

标签: c# enums

我想按自定义顺序对代码列表(两个字符)进行排序。我想到了一个枚举:

    public static enum AlphaCode
    {
        Q, N, A, C
    }

我有一个代码列表,我希望按此顺序排序。

List<string> Codes = new List<string>() { "qc", "aq", "nc", "ac" }; 我希望它们按顺序排列为AlphaCode,因此它们将按如下方式输出:

qc nc aq ac

我只列出了一些字母,但AlphaCode将按特定顺序包含所有字母,还有一些特殊字符(如点,逗号,分号,括号)。所以也许枚举不是一个好的选择。

关于如何实现这一目标的任何想法?

1 个答案:

答案 0 :(得分:3)

由于您希望在自定义排序顺序中包含非字母字符,因此使用集合类型而不是enum来存储订单可能更有意义。例如,如果您使用ListArray,则只需使用索引作为排序键。

然后,为了启用自定义排序,您可以编写一个方法,该方法接收两个字符串并返回一个int,该int定义哪个字符串首先出现,并且可以将其传递给{{1}的Sort方法}}。如果第一个字符串“小于”第二个字符串,则返回List,如果它们相等则返回-1,如果第一个字符串“大于”,则返回0第二个。

以下是代码的自定义比较方法示例:

1

现在你可以很容易地使用这个类了:

private static int CodeComparer(string first, string second)
{
    // Short-circuit if one or both strings are null
    if (first == null) return (second == null) ? 0 : -1;
    if (second == null) return 1;

    // The following list should contain all characters in their desired sort order.
    // NOTE: You will need to include UPPERCASE characters as well if expected.
    //    If you want to treat UPPER CASE the same as lower case, then you would call 
    //    ToLower on first and second, and just have lower case characters in this list.
    var sortOrder = new List<char> { 'q', 'n', 'a', 'c', '-', '3', '2', '1' };

    // Since we will loop through both strings one character at a time,
    // we need to get the length of the shorter string (to prevent index out of range)
    int shortestLength = Math.Min(first.Length, second.Length);

    for (int i = 0; i < shortestLength; i++)
    {
        if (first[i] != second[i])
        {
            // When we find two characters that don't match, return the 
            // comparison of their respective indexes in our master list
            return sortOrder.IndexOf(first[i]).CompareTo(sortOrder.IndexOf(second[i]));
        }
    }

    // If all the characters matched, compare the length of the strings. 
    // Normally the shortest comes first, so we do second.CompareTo(first)
    return second.Length.CompareTo(first.Length);
}

输出:

  

qc,nc,aq,ac,-n,3c,1a