简体   繁体   English

如何对列表进行排序<string>任意大数?</string>

[英]How to sort a List<String> of arbitrary large numbers?

In C#, how can you sort a list of large numbers in such a way that the negative numbers with a - minus-character are also in the right order?在 C# 中,如何对大数列表进行排序,以使带有-减号的负数也按正确的顺序排列?

I know an int can hold numbers with max.我知道一个int可以容纳最大的数字。 10 digits and ulong can hold about 20 digit numbers. 10位数字和ulong可以容纳大约20位数字。 But I have a list of numbers with 24~30 digits, including negative numbers.但我有一个 24~30 位数的数字列表,包括负数。

I figured the way to do this is add a string that is padded with 0 's and then sort on that new string.我想办法做到这一点是添加一个用0填充的字符串,然后对该新字符串进行排序。 So 1234 becomes 0001234 .所以1234变成0001234 And for negative numbers -567 is becomes 9999432 because then it is sorted inversely.对于负数, -567变为9999432 ,因为它是反向排序的。 See code below请参阅下面的代码

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

}

The function that creates the sortable string looks like this.创建可排序字符串的 function 如下所示。

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

And the result is close to what I want, but not quite.结果接近我想要的,但并不完全。

Key sorted                        String value
000000072413799900717492396359 ->   72413799900717492396359
000000123191989931658420157210 ->  123191989931658420157210
000000123456789012345678901234 ->  123456789012345678901234
000000554706403711546488433874 ->  554706403711546488433874
000000877873677336436172875781 ->  877873677336436172875781
999999117333643978842754548674 -> -882666356021157245451325
999999304782265623077670029500 -> -695217734376922329970499
999999324700497302451910701581 -> -675299502697548089298418
999999938690717001834936299708 ->  -61309282998165063700291
999999989923583596183629788363 ->  -10076416403816370211636

The sorted string values should be in this order:排序后的字符串值应按以下顺序排列:

-882666356021157245451325
-695217734376922329970499
-675299502697548089298418
 -61309282998165063700291
 -10076416403816370211636
  72413799900717492396359
 123191989931658420157210
 123456789012345678901234
 554706403711546488433874
 877873677336436172875781

Is there a way to get this result, by some extra sort option or something lke that?有没有办法通过一些额外的排序选项或类似的东西来获得这个结果? Or is there maybe a better way to go about this?或者有没有更好的方法来 go 关于这个?

As mentioned in the comment System.Numeric.BigInteger is the way to go.正如评论中提到的System.Numeric.BigInteger是 go 的方式。 All you have to do is:你所要做的就是:

IEnumerable<string> sorted123 = Values123.OrderBy(v => BigInteger.Parse(v));

And you get the result:你得到结果:

-882666356021157245451325
-695217734376922329970499
-675299502697548089298418
-61309282998165063700291
-10076416403816370211636
72413799900717492396359
123191989931658420157210
123456789012345678901234
554706403711546488433874
877873677336436172875781

Full example with .NET Fiddle here . .NET Fiddle here的完整示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM