I have sorted collection (List) and I need to keep it sorted at all times.
I am currently using List.BinarySearch on my collection and then insert element in right place. I have also tried sorting list after every insertion but the performance in unacceptable.
Is there a solution that will give better performance? Maybe I should use other collection.
(I am aware of SortedList but it is restricted to unique keys)
PowerCollections has an
OrderedBagtype which may be good for what you need. From the docsHowever, for the .NET 3.5 built in types, using
List.BinarySearchand inserting each item into the correct place is a good start – but that uses an Array internally so your performance will drop due to all the copying you’re doing when you insert.If you can group your inserts into batches that will improve things, but unless you can get down to only a single sort operation after all your inserting you’re probably better off using
OrderedBagfromPowerCollectionsif you can.