开发者

Sorting List<T> inside subclass with LINQ

I know I can sort a list doing something like this

var result = List<T>.OrderBy(value => value.property);

But lets say I have something like this

    class Stock
    {
        public Guid ID { get; set; }
        public string Description { get; set; }
    }

    class StockList : List<Stock>
    {
        public enum SomeEnum
        {
            SomeOtherValue = 0,
            SomeOtherOtherValue
        }
        //What if I want method like this using LINQ?
        public void Sort(SomeEnum sortBy)
        {
            switch (sortBy) 
            {
                case SomeValue.SomeOtherOtherValue:
                    //Sort one way
                    开发者_如何学Pythonbreak;
                case SomeValue.SomeOtherValue:
                    //Sort another
                    break;
            }
        }
    }

this.OrderBy() (and I assume the other LINQ extentions) return an OrderedCollection and doesn't seem to affect the original. So I assume I'm going about this the wrong way?


You can use List.Sort( ). This will do an in-place sort of the list.


You can do

this.Sort(new Comparison<Stock>((x,y) => x.Description.CompareTo(y.Description)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜