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

前端 未结 5 620
渐次进展
渐次进展 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> listOfLists = new List>();
    
            List list1 = new List();
            list1.Add("elem 1 1");
            list1.Add("elem 1 2");
            list1.Add("elem 1 3");
    
            List list2 = new List();
            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));
    

提交回复
热议问题