BdR*_*BdR 2 c# sorting largenumber
在 C# 中,如何对大数列表进行排序,使得带有-减号字符的负数也按正确的顺序排列?
我知道一个int可以容纳最多的数字。10位数字,ulong可容纳约20位数字。但我有一个24~30位数字的列表,其中包括负数。
我认为执行此操作的方法是添加一个用0' 填充的字符串,然后对该新字符串进行排序。于是就1234变成了0001234。对于负数-567则变为,9999432因为它是逆序排序的。请参阅下面的代码
private void TestingList()
{
// test values
List<string> Values123 = new List<string> {
"123456789012345678901234",
"-61309282998165063700291",
"72413799900717492396359",
"-10076416403816370211636",
"123191989931658420157210",
"-675299502697548089298418",
"554706403711546488433874",
"-882666356021157245451325",
"877873677336436172875781",
"-695217734376922329970499"
};
//List<string> Values123 = new List<string> {"2222", "4444", "3333", "1111", "5555"};
// create a sortable list, of type List<String, String>
var Test123 = new List<KeyValuePair<string, string>>();
foreach (var v in Values123)
{
Test123.Add(new KeyValuePair<string, string>(sortableValue(v), v));
}
// sort the list oin the sortable key
var Sorted123 = Test123.OrderBy(x => x.Key).ToList();
// print list
foreach (var s in Sorted123)
{
Console.WriteLine(String.Format("{0} -> {1}", s.Key, s.Value));
}
}
Run Code Online (Sandbox Code Playgroud)
创建可排序字符串的函数如下所示。
private string sortableValue(string val)
{
if (val.IndexOf('-') < 0)
// not negative
return val.PadLeft(30, '0');
else
{
// negative numbers
var ret = "";
foreach (char c in val) {
var test123 = c;
if ((c >= '0') && (c <= '9')) {
// '0'..'9' = ascii 48..57
//c = Convert.ToChar(48+57 - c);
test123 = (char)(48 + 57 - c);
} else if (c == '-') {
test123 = '9';
}
ret += test123;
}
return ret.PadLeft(30, '9');
};
}
Run Code Online (Sandbox Code Playgroud)
结果接近我想要的,但不完全是。
Key sorted String value
000000072413799900717492396359 -> 72413799900717492396359
000000123191989931658420157210 -> 123191989931658420157210
000000123456789012345678901234 -> 123456789012345678901234
000000554706403711546488433874 -> 554706403711546488433874
000000877873677336436172875781 -> 877873677336436172875781
999999117333643978842754548674 -> -882666356021157245451325
999999304782265623077670029500 -> -695217734376922329970499
999999324700497302451910701581 -> -675299502697548089298418
999999938690717001834936299708 -> -61309282998165063700291
999999989923583596183629788363 -> -10076416403816370211636
Run Code Online (Sandbox Code Playgroud)
排序后的字符串值应按以下顺序排列:
-882666356021157245451325
-695217734376922329970499
-675299502697548089298418
-61309282998165063700291
-10076416403816370211636
72413799900717492396359
123191989931658420157210
123456789012345678901234
554706403711546488433874
877873677336436172875781
Run Code Online (Sandbox Code Playgroud)
有没有办法通过一些额外的排序选项或类似的东西来获得这个结果?或者有更好的方法来解决这个问题吗?
编辑:
我刚刚意识到我还可以在排序键中添加一个额外的0或9来翻转正值和负值并获得所需的排序。因此,像这样更改两return行(仍然有点hacky,但它完成了工作):
if (val.IndexOf('-') < 0)
return "9" + val.PadLeft(30, '0'); // positive numbers
//etc.
return "0" + ret.PadLeft(30, '9'); // negative numbers
Run Code Online (Sandbox Code Playgroud)
正如评论中提到的,System.Numeric.BigInteger这是要走的路。您所要做的就是:
IEnumerable<string> sorted123 = Values123.OrderBy(v => BigInteger.Parse(v));
Run Code Online (Sandbox Code Playgroud)
你会得到结果:
-882666356021157245451325
-695217734376922329970499
-675299502697548089298418
-61309282998165063700291
-10076416403816370211636
72413799900717492396359
123191989931658420157210
123456789012345678901234
554706403711546488433874
877873677336436172875781
Run Code Online (Sandbox Code Playgroud)
.NET Fiddle 的完整示例请参见此处。