Native C# support for checking if an IEnumerable is sorted?

前端 未结 5 1804
不知归路
不知归路 2020-12-19 13:00

Is there any LINQ support for checking if an IEnumerable is sorted? I have an enumerable that I want to verify is sorted in non-descending order, but I

5条回答
  •  一个人的身影
    2020-12-19 13:21

    There is no such built-in support.

    Obviously if your IEnumerable also implements IOrderedEnumerable then you don't need to do an additional check, otherwise you'd have to implement an extension method like you did.

    You might want to add a direction parameter or change its name to IsSortedAscending, by the way. Also, there might be different properties in your T to sort on, so it would have to be obvious to you in some way what "sorted" means.

提交回复
热议问题