我试图找到一种有效的方法来根据数组的每个字符串元素中的数值对字符串数组进行排序.我目前正在使用Array.Sort(array,customComparer)静态方法(快速排序),我的自定义比较器类(按降序排序)是:
class StringComparer : IComparer<string>
{
public int Compare(string a, string b)
{
string s1 = a;
string s2 = b;
Match matchA = Regex.Match(s1, @"\d+$");
Match matchB = Regex.Match(s2, @"\d+$");
long numberA = long.Parse(matchA.Value);
long numberB = long.Parse(matchB.Value);
if (numberB - numberA < 0)
{
return -1;
}
else
{
return 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法非常有效,但有时需要花费太多时间进行排序,在一台2.4Ghz处理器上使用超过一分钟的10万个字符串数组.我想知道是否有更有效的方法来实现同样的目标.例如,实现不同的排序算法或采用另一种方法,例如使用字典并对值进行排序(值是字符串的数字部分).有什么建议?提前致谢!