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)));
精彩评论