Split a list into multiple lists at increasing sequence broken

前端 未结 9 1120
不知归路
不知归路 2021-02-18 19:11

I\'ve a List of int and I want to create multiple List after splitting the original list when a lower or same number is found. Numbers are not in sorted order.<

9条回答
  •  孤独总比滥情好
    2021-02-18 20:01

    I have modified your code, and now working fine:

            List data = new List { 1, 2, 1, 2, 3,3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6 };
            List> resultLists = new List>();
            int last = 0;
            int count = 0;
    
            var res = data.Where((p, i) =>
            {
                if (i > 0)
                {
                    if (p > last && p!=last)
                    {
                        resultLists[count].Add(p);
                    }
                    else
                    {
                        count++;
                        resultLists.Add(new List());
                        resultLists[count].Add(p);
                    }
                }
                else
                {
                    resultLists.Add(new List());
                    resultLists[count].Add(p);
                }
    
    
    
                last = p;
                return true;
            }).ToList();
    

提交回复
热议问题