Split a list into multiple lists at increasing sequence broken

前端 未结 9 1121
不知归路
不知归路 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:05

    This is not a typical LINQ operation, so as usual in such cases (when one insists on using LINQ) I would suggest using Aggregate method:

    var result = data.Aggregate(new List>(), (r, n) =>
    {
        if (r.Count == 0 || n <= r.Last().Last()) r.Add(new List());
        r.Last().Add(n);
        return r;
    });
    

提交回复
热议问题