简体   繁体   English

C#在列表中对多个数字进行排序。 按第二个字符串排序

[英]C# sorting multiple numbers in a list. Sort by second string

i am trying to make a list of things and trying to so 我正在尝试列出事情并试图做到

        var l = new List<string[]>() {
                            new string[] { "10945730333", "5"},
                            new string[] { "298543022", "234"},
                            new string[] { "382958320", "35"},
                            };
        var result = l.OrderBy(f => f[1]).ToList();
        MessageBox.Show($"{result}");

i want it so it sorts the second number by lowest to highest. 我想要它,所以它按从低到高的顺序排列第二个数字。 so that the 234 is on the bottom. 因此234位于底部。 When i do use it it comes up with "System.Collections.Generic.List`1[System.String[]]" 当我使用它时,它会出现“ System.Collections.Generic.List`1 [System.String []]”

First, your sorting is incorrect, you should parse the string to an int otherwise you'll be performing a string comparison which is not what you want. 首先,排序不正确,应将string解析为int否则将执行不需要的字符串比较。

 var result = l.OrderBy(f => int.Parse(f[1])).ToList();

Then you can get the result like so: 然后您可以得到如下结果:

MessageBox.Show(string.Join("\n",result.SelectMany(e => e)));

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

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