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

前端 未结 5 614
渐次进展
渐次进展 2021-01-22 15:22

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

The list got structure like

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


        
相关标签:
5条回答
  • 2021-01-22 16:18

    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

            int nullListElems = -1; 
            //int nullListElems = 0; 
    
            listOfLists.Sort((l1,l2)=> 
                l1 == null ? 
                nullListElems : 
                l1.Count.CompareTo(
                    l2 == null ? 
                    nullListElems : 
                    l2.Count));
    
    0 讨论(0)
  • 2021-01-22 16:20
    List<List<string>> list; // filled elsewhere
    list.Sort((x,y) => x.Count.CompareTo(y.Count););
    
    0 讨论(0)
  • 2021-01-22 16:24

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

    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.

    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.

    0 讨论(0)
  • 2021-01-22 16:25

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

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

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

    Edit

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

    ls.Sort((x, y) => -1 * x.Count.CompareTo(y.Count));
    
    0 讨论(0)
  • 2021-01-22 16:27
    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.

    0 讨论(0)
提交回复
热议问题