Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a sorted collection type in .NET?

Tags:

I'm looking for a container that keeps all its items in order. I looked at SortedList, but that requires a separate key, and does not allow duplicate keys. I could also just use an unsorted container and explicitly sort it after each insert.

Usage:

  • Occasional insert
  • Frequent traversal in order
  • Ideally not working with keys separate from the actual object, using a compare function to sort.
  • Stable sorting for equivalent objects is desired, but not required.
  • Random access is not required.

I realize I can just build myself a balanced tree structure, I was just wondering if the framework already contains such a beast.

like image 261
Eclipse Avatar asked Oct 13 '08 02:10

Eclipse


People also ask

Are there sorted lists in C#?

In C#, SortedList is a collection of key/value pairs which are sorted according to keys. By default, this collection sort the key/value pairs in ascending order. It is of both generic and non-generic type of collection.

What are different types of collections in net?

NET supports two types of collections, generic collections and non-generic collections. Prior to . NET 2.0, it was just collections and when generics were added to . NET, generics collections were added as well.

Which is the sorted collection?

The sorted() Method in JavaThe sorted() method used to sort the list of objects or collections of the objects in the ascending order. If the collections of the objects are comparable then it compares and returns the sorted collections of objects; otherwise it throws an exception from java. lang.

How do you sort collections in C#?

Sort(IComparer) This method is used to sort the elements in the entire ArrayList using the specified comparer. This method is an O(n log n) operation, where n is Count; in the worst case, it is an O(n^2) operation. Syntax: public virtual void Sort (IComparer comparer);


1 Answers

You might want to take a look at the Wintellect Power Collections. It is available on CodePlex and contains quite a few collections that are very helpful. The OrderedBag collection in the project is exactly what you are looking for. It essentially uses a red-black tree to provide a pretty efficient sort.

like image 96
JeremiahClark Avatar answered Sep 28 '22 03:09

JeremiahClark