Here’s my setup.
public class ItemList : List<Item>
{
public void Load() {...}
public void Save() {...}
}
Load reads from an XML file to populate the ItemList
I then attempt to order the item list by a Priority. This is an int? For the test purposes however all the items have a different value.
ItemList itemList = new ItemList();
itemList.Load();
ItemList newItemList = itemList
.OrderBy(item => item.Priority) as ItemList;
return newItemList;
In the above newItemList is always null. itemList has a Count of 7. I’ve triple checked and all the items in the itemList instance have a priority set.
What am I doing wrong?
I’ve also tried…
ItemList newItemList = itemList
.OrderBy(item => item.Priority)
.ToList() as ItemList;
Nothing seems to be working.
Thanks in advance!
Neither OrderBy or ToList returns an ItemList, so the casting returns null, as Joseph pointed out.
As you are inheriting from the
List<T>class, you can just use the Sort method to sort it: