在列表中查找数字开始上升的索引

Dea*_*ean -2 c# linq

说我有一个数字列表,我想知道列表中的数字开始减少的位置,没有特别的顺序,一个例子是理想的!

1,
2,
2,
3,
3,
4,
4,
5,
5,
4, <= this should be my output
4,
3,
3,
2,
2,
1,
Run Code Online (Sandbox Code Playgroud)

谢谢

Mar*_*zek 6

您可以创建自己的扩展方法 IEnumerable<TSource>

public static class MyEnumerable
{
    public static IEnumerable<TSource> Descending<TSource>(this IEnumerable<TSource> source)
        where TSource : IComparable<TSource>
    {
        using (var e = source.GetEnumerator())
        {
            TSource previous;
            if (e.MoveNext())
            {
                previous = e.Current;
                while (e.MoveNext())
                {
                    if (previous.CompareTo(e.Current) > 0)
                        yield return e.Current;
                    previous = e.Current;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

var input = new List<int>() { 1, 2, 3, 2, 4, 5, 6, 7, 8, 9 };

var firstDescending = input.Descending().First();
Run Code Online (Sandbox Code Playgroud)