自定义字符顺序
Custom char order
我想按自定义顺序对代码列表(两个字符)进行排序。我想到了一个枚举:
public static enum AlphaCode
{
Q, N, A, C
}
我有一个代码列表,我想按此顺序排序。
List<string> Codes = new List<string>() { "qc", "aq", "nc", "ac" };
我希望它们按 AlphaCode
的顺序排列,因此它们将像这样输出:
qc
nc
aq
ac
我只列出了一些字母,但 AlphaCode
将包含所有按特定顺序排列的字母,还有一些特殊字符(如点、逗号、分号、括号)。所以也许枚举不是一个好的选择。
关于如何实现这一点有什么想法吗?
由于您希望在自定义排序顺序中包含非字母字符,因此使用集合类型而不是 enum
来存储顺序可能更有意义。例如,如果您使用 List
或 Array
,您可以简单地使用索引作为排序的关键字。
然后,要启用自定义排序,您可以编写一个方法,该方法接受两个字符串和 returns 一个定义哪个字符串先出现的 int,并且可以将其传递给 Sort
List
的方法。该方法应该采用两个字符串,如果第一个字符串是 "less than" 第二个字符串 return -1
,如果它们相等则 0
,或者如果第一个字符串是 1
是"greater than"第二个。
这是您的代码的自定义比较方法示例:
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);
}
现在你可以很容易地使用这个 class:
private static void Main()
{
var codes = new List<string>() { "qc", "aq", "nc", "ac", "1a", "3c", "-n" };
codes.Sort(CodeComparer);
Console.WriteLine(string.Join(", ", codes));
}
输出:
qc, nc, aq, ac, -n, 3c, 1a
我想按自定义顺序对代码列表(两个字符)进行排序。我想到了一个枚举:
public static enum AlphaCode
{
Q, N, A, C
}
我有一个代码列表,我想按此顺序排序。
List<string> Codes = new List<string>() { "qc", "aq", "nc", "ac" };
我希望它们按 AlphaCode
的顺序排列,因此它们将像这样输出:
qc
nc
aq
ac
我只列出了一些字母,但 AlphaCode
将包含所有按特定顺序排列的字母,还有一些特殊字符(如点、逗号、分号、括号)。所以也许枚举不是一个好的选择。
关于如何实现这一点有什么想法吗?
由于您希望在自定义排序顺序中包含非字母字符,因此使用集合类型而不是 enum
来存储顺序可能更有意义。例如,如果您使用 List
或 Array
,您可以简单地使用索引作为排序的关键字。
然后,要启用自定义排序,您可以编写一个方法,该方法接受两个字符串和 returns 一个定义哪个字符串先出现的 int,并且可以将其传递给 Sort
List
的方法。该方法应该采用两个字符串,如果第一个字符串是 "less than" 第二个字符串 return -1
,如果它们相等则 0
,或者如果第一个字符串是 1
是"greater than"第二个。
这是您的代码的自定义比较方法示例:
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);
}
现在你可以很容易地使用这个 class:
private static void Main()
{
var codes = new List<string>() { "qc", "aq", "nc", "ac", "1a", "3c", "-n" };
codes.Sort(CodeComparer);
Console.WriteLine(string.Join(", ", codes));
}
输出:
qc, nc, aq, ac, -n, 3c, 1a