简体   繁体   English

C#使用LINQ对char数组进行排序

[英]C# Sort List of char arrays using LINQ

I'm trying to sort the following List List<char[]> permutations = new List<char[]>(); 我试图排序以下列表List<char[]> permutations = new List<char[]>();

it contains all the permutations of the number 0,1,2,3,4,5,6,7,8,9 however they are not sorted but I need them sorted. 它包含数字0、1、2、3、4、5、6、7、8、9的所有排列,但是它们没有排序,但我需要对其进行排序。 This is what I did to fix my problem : 这是我解决问题的方法:

        permutations = permutations.OrderBy(arr1 => arr1[9]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[8]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[7]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[6]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[5]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[4]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[3]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[2]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[1]).ToList();
        permutations = permutations.OrderBy(arr1 => arr1[0]).ToList();

how can I avoid this or how can this be written in 1 line ? 如何避免这种情况或如何将其写成一行?

First I would note that you are re-sorting the list 9 times. 首先,我要注意的是您对列表进行了9次重新排序。

The proper way to chain orderings is to use ThenBy (note that the order of comparisons is reversed to get the results to order in the same manner that you have now): 链接排序的正确方法是使用ThenBy (请注意,比较顺序相反,以与现在相同的方式将结果排序):

    permutations = permutations.OrderBy(arr1 => arr1[0])
                               .ThenBy(arr1 => arr1[1])
                               .ThenBy(arr1 => arr1[2])
                               .ThenBy(arr1 => arr1[3])
                               .ThenBy(arr1 => arr1[4])
                               .ThenBy(arr1 => arr1[5])
                               .ThenBy(arr1 => arr1[6])
                               .ThenBy(arr1 => arr1[7])
                               .ThenBy(arr1 => arr1[8])
                               .ThenBy(arr1 => arr1[9]).ToList();

One simple way to reduce the amount of code is 减少代码量的一种简单方法是

permutations = permutations.OrderBy(a => new string(a)).ToList();

Or to just order the list in-place. 或者只是就地订购列表。

permutations.Sort((a1, a2) => (new string(a)).CompareTo(new string(a2)));

Granted those generate a lot of strings, but the next best option is to write an IComparer<char[]> that compares two character arrays the way you want it to. 授予它们生成很多字符串,但第二个最佳选择是编写一个IComparer<char[]> ,该方法以您希望的方式比较两个字符数组。 One other option (since I know that the list of character arrays came from a different question) would by so store the permutations as strings instead of arrays . 另一个选择(因为我知道字符数组的列表来自另一个问题)将排列存储为字符串而不是数组 Then sorting is a no-brainer: 然后排序就很容易了:

permutations.Sort();

This is pretty old but I was working on this and have a solution that is suggested in comments but not shown. 这已经很老了,但是我正在努力解决这个问题,并提出了建议的解决方案,但未显示。 As suggested by George , the dynamic way to handle this is to use a for loop but there is one gotcha as noted here OrderBy / ThenBy in Loop . 正如George所建议的那样,处理此问题的动态方法是使用for循环,但是在Loop中有一个OrderBy / ThenBy指出了这一点。

This is much faster than converting each char[] to a string as noted in the other comments and answers. 这比将每个char []转换为字符串要快得多,如其他注释和答案所述。

var p = permutations.OrderBy(x => x[0]);
for (int i = 1; i < s.Length; i++)
{
    var index = i; //Must do this to Avoid Gotcha
    p = p.ThenBy(x => x[index]);
}
permutations = p.ToList();

Just use string instead of char[] which is basically IEnumerable<char> In this way you can simply write 只需使用string代替char[] ,而char[]基本上是IEnumerable<char>这样,您可以简单地编写

permutations.Sort();

if you want to keep it your way you can chain your expressions like: 如果您想以自己的方式保留它,可以将表达式链接起来,例如:

permutations = permutations.OrderBy(arr1 => arr1[9])
    .ThenBy(arr1 => arr1[8])
    .ThenBy(arr1 => arr1[7])
    .ThenBy(arr1 => arr1[6])
    .ThenBy(arr1 => arr1[5])
    .ThenBy(arr1 => arr1[4])
    .ThenBy(arr1 => arr1[3])
    .ThenBy(arr1 => arr1[2])
    .ThenBy(arr1 => arr1[1])
    .ThenBy(arr1 => arr1[0])
    .ToList();

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

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