I sorted a dictionary like this:
var sortedListOfNodes = _nodeDictionary.Values.OrderBy((n) => n.Time);
Then I selected an element as such:
var selectedNode = sortedListOfNodes.First(n => n.Time - CurrentTime > new TimeSpan(1,0,0));
Then I did some processing on that node and at the end wanted to remove the node from the list, without destroying the sorted order.
Would the below maintain the order?
sortedListOfNodes = (IOrderedEnumerable<Node>)sortedListOfNodes.Where(node => node != selectedNode);
Add a call to
ToList
afterOrderBy
. Now you have a list you can manipulate (assuming you don't insert items it will stay in order).On a side note your example of casting the result of
Where
toIOrderedEnumerable<Node>
would fail at runtime with a casting failure. TheWhere
you are calling is a concrete type that does not implement that interface.