I am currently using the following code to sort a dictionary:
Dictionary<string, TKosStock> NewList = null;
if (SortBy.CompareTo(TSortOptions.sortASC | TSortOptions.sortPRICE) == 0)
NewList = new Dictionary<string, StockObj>(this.StockItemList.OrderBy(n => n.Value.SellPriceRangeLowGross).ToDictionary(n => n.Key, n => n.Value));
this.StockItemList = new Dictionary<string, TKosStock>(NewList.ToDictionary(n => n.Key, n => n.Value));
This works well and re-shuffles my list correctly. I can then loop through this.StockItemList knowing my values are in the correct order.
What I would like to do is add the items in the correct order in the first place. I can’t do this based on the SQL query because the prices aren’t calculated when the SQL query initially runs.
p.s I assume the overhead for this re-shuffle is low but if it can be done when initially adding the items then I assume the overhead would be even lower.
Is this possible?
Perhaps using an ordered dictionary (non-generic):
http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx