Sorting on insertion with a comparing function

c#, comparison, insert, sorting

Solution

If you're using .NET 4, you could use `SortedSet` with a custom `IComparer<T>`. The downside is that it won't allow you to have multiple equal elements. Do you need that?

It's not clear to me why you want sorting on insertion just because you've got a lot of data though. Do you need it to be sorted before you've finished inserting? If not, I'd expect a single sort at the end (via `List.Sort`) to be as efficient as an as-you-go sort.

Problem

I'm trying to sort data of a type T in a container by two of T's properties. It's potentially a lot of data, so I would much rather have the sorting happen on insertion. I've looked into both `List` and `SortedList`, but both don't quite provide the functionality I need. Does C# provide a container that allows both sorting on insertion and sorting my a comparison function? I would like to avoid post insertion sorting like `List.Sort`, and avoid the overhead of using the data as both key and value for `SortedList`.

Original source