Split a list into multiple lists at increasing sequence broken

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

    This is my simple loop approach using some yields :

    static IEnumerable> Split(IList data) { if (data.Count == 0) yield break; List curr = new List(); curr.Add(data[0]); int last = data[0]; for (int i = 1; i < data.Count; i++) { if (data[i] <= last) { yield return curr; curr = new List(); } curr.Add(data[i]); last = data[i]; } yield return curr; }

提交回复
热议问题