C# 如何根据 List<string> 字段数对 List<List<string>> 进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/721577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:29:42  来源:igfitidea点击:

How to sort List<List<string>> according to List<string> number of fields?

c#listsorting

提问by Skuta

How do I sort List<List<string>> according to number of fields?

如何根据字段数对 List<List<string>> 进行排序?

The list got structure like

该列表的结构如下

a|b|c|d|eeee|rere|ewew|
ewqewq|ewew|
ewew|ewewewewew|

By Sort I'd like to do it according to numbers of blockes (asc/desc)

通过排序我想根据块的数量(asc/desc)来做

EDIT

编辑

the <list<list<string>> is "items"and I access each list by items[0] each of

<list<list<string>> is "items"我访问由项每个列表[0]的每

these items[xx] is a list which means that I want them to sort the array to

这些 items[xx] 是一个列表,这意味着我希望他们对数组进行排序

    a|b|c|d|eeee|rere|ewew|
    a|b|c|d|eeee|rere
    a|b|c|d|eeee

采纳答案by tvanfosson

If you don't need to sort "in place" you can use LINQ to give you a new sorted, list.

如果您不需要“就地”排序,您可以使用 LINQ 为您提供一个新的排序列表。

var sorted = oldList.OrderBy( l => l.Count );

Otherwise you'll need to write your own Comparer that takes two lists and returns the ordering by their size.

否则,您将需要编写自己的比较器,它接受两个列表并按其大小返回排序。

public class CountComparer : IComparer<List<string>>
{
    #region IComparer<List<string>> Members

    public int Compare( List<string> x, List<string> y )
    {
        return x.Count.CompareTo( y.Count );
    }

    #endregion
}



oldList.Sort( new CountComparer() );

Or, as @Josh points out, you can do this with a lambda expression.

或者,正如@Josh 指出的那样,您可以使用 lambda 表达式来做到这一点。

oldList.Sort( (a,b) => a.Count.CompareTo( b.Count ) )

IMO this latter works well if the comparison is relatively simple or used once, but the actual class may be preferable as the comparison gets more complex or if you need to repeat it in multiple places.

如果比较相对简单或使用一次,IMO 后者效果很好,但实际类可能更可取,因为比较变得更复杂,或者如果您需要在多个地方重复它。

回答by Adam Robinson

List<List<string>> strings = new List<List<string>>();

// add your strings

strings = new List<List<string>>(from str in strings orderby str.Count select str);

Obviously you can adapt this to fit your scenario, but this should give you the general idea of how to create a list that's sorted by the number of elements within a particular string in another list.

显然,您可以调整它以适应您的场景,但这应该让您大致了解如何创建一个列表,该列表按另一个列表中特定字符串中的元素数量排序。

回答by JoshBerke

You can sort using the sort method, by creating your own Compararer or giving it a delegate of type Comparison<T>.

您可以使用 sort 方法进行排序,方法是创建您自己的 Comparerer 或给它一个 type 委托Comparison<T>

ls.Sort((x, y) => x.Count.CompareTo(y.Count));

Here's an example showing how to compare using a lambda expression.

这是一个示例,展示了如何使用 lambda 表达式进行比较。

Edit

编辑

A very simple way to reverse the sort is to multiply by negative -1

反转排序的一个非常简单的方法是乘以负 -1

ls.Sort((x, y) => -1 * x.Count.CompareTo(y.Count));

回答by Chris Doggett

List<List<string>> list; // filled elsewhere
list.Sort((x,y) => x.Count.CompareTo(y.Count););

回答by Sergiu Mindras

Just a minor adjustment, checking for null while also

只是一个小的调整,同时检查空值

        List<List<string>> listOfLists = new List<List<string>>();

        List<string> list1 = new List<string>();
        list1.Add("elem 1 1");
        list1.Add("elem 1 2");
        list1.Add("elem 1 3");

        List<string> list2 = new List<string>();
        list2.Add("elem 2 1");
        list2.Add("elem 2 2");
        list2.Add("elem 2 3");
        list2.Add("elem 2 4");

        listOfLists.Add(list1);
        listOfLists.Add(null); // list can contain nulls
        listOfLists.Add(list2);
        listOfLists.Add(null); // list can contain nulls

When you have null lists in the list of lists, you might want to make them lower priority than empty lists, by interpreting null.Count as -1, or NOT

当列表列表中有空列表时,您可能希望通过将 null.Count 解释为 -1 或 NOT 来使它们的优先级低于空列表

        int nullListElems = -1; 
        //int nullListElems = 0; 

        listOfLists.Sort((l1,l2)=> 
            l1 == null ? 
            nullListElems : 
            l1.Count.CompareTo(
                l2 == null ? 
                nullListElems : 
                l2.Count));