How to implement Linq OrderBy method?

前端 未结 2 1714
心在旅途
心在旅途 2021-02-08 13:32

I am trying to understand more about linq, for example, if I want to implement a Select I will implement like this

public static IEnumerable Selec         


        
相关标签:
2条回答
  • 2021-02-08 14:17

    To follow you current implementation pattern you could try this:

    public static IEnumerable<TSource> OrderBy<TSource, TKey>(
        IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        var items = source.ToArray();
        var keys = items.Select(keySelector).ToArray();
        Array.Sort(keys, items);
        foreach (var item in items)
        {
            yield return item;
        }
    }
    
    0 讨论(0)
  • 2021-02-08 14:23

    Take a look at this. I think you'll find it very useful. Basically, Jon Skeet re-implements everything in Linq as a learning exercise. Very informative.

    The second part talks about implementing Where... and so on till parts that describes OrderBy.

    0 讨论(0)
提交回复
热议问题