I am using this LINQ statement to sort a list by product name (ascending order) which contains product names (string) and Sizes available for each product (List<byte>);
LinkedList<FullItemDetails> itemDetails = new LinkedList<FullItemDetails>();
public class FullItemDetails
{
public string ProductName { get; set; }
public List<byte> Sizes { get; set; }
}
Now every time I input a new entry ex; Jacket,6,12,18,10, I think my program is sorting my list all over again;
itemDetails.AddLast(fullItemDetails);
//SortedProducts
itemDetails = Products.OrderBy(x => x.ProductName).ToList();
If the list is already sorted I only need to put the last entry in its correct place.
What is the best way to do it. Also to reduce the complexity of the algorithm. thanks
This seems like an ideal problem for a
SortedList, as you have a key (name) and value (List<int>for the size).Documentation is available here: http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.aspx
The list declaration would look like this:
SortedList<string, List<int> >. All inserts would be sorted on string, and the values can be enumerated based on each key.