C#使用LINQ对char数组列表进行排序(C# Sort List of char arrays using LINQ)

系统教程 行业动态 更新时间:2024-06-14 17:04:02
C#使用LINQ对char数组列表进行排序(C# Sort List of char arrays using LINQ)

我正在尝试对以下列表List<char[]> permutations = new List<char[]>();进行排序List<char[]> permutations = new List<char[]>();

它包含数字0,1,2,3,4,5,6,7,8,9的所有排列,但它们没有排序,但我需要它们排序。 这就是我为解决问题所做的工作:

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

我怎样才能避免这种情况,或者如何在一行中写出来?

I'm trying to sort the following List 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. 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 ?

最满意答案

首先我要注意你要重新排序列表9次。

链接排序的正确方法是使用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();

减少代码量的一种简单方法是

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

或者只是按顺序排列清单。

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

虽然那些生成了很多字符串,但下一个最好的选择是编写一个IComparer<char[]> ,它按照你想要的方式比较两个字符数组。 另一个选择(因为我知道字符数组的列表来自一个不同的问题)因此将排列存储为字符串而不是数组 。 然后排序是明智的:

permutations.Sort();

First I would note that you are re-sorting the list 9 times.

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):

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. 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();

更多推荐

本文发布于:2023-04-24 20:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/bc15a5b702c4c4708983569a8140d8ac.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   列表   char   LINQ   List

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!