Split a list into multiple lists at increasing sequence broken

前端 未结 9 1146
不知归路
不知归路 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 19:58

    You can do it with Linq using the index to calculate the group:

    var result = data.Select((n, i) => new { N = n, G = (i > 0 && n > data[i - 1] ? data[i - 1] + 1 : n) - i })
                     .GroupBy(a => a.G)
                     .Select(g => g.Select(n => n.N).ToArray())
                     .ToArray();
    

提交回复
热议问题