O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Use a Sorted List

Problem

You want to use a collection that is automatically sorted every time an item is added or removed.

Solution

Use the System.Collections.SortedList type, which sorts items based on the key.

Discussion

The SortedList class is a key-based dictionary collection that stores items in a perpetually ordered state. That makes it slower when adding or removing items. However, using a SortedList collection is usually faster than continuously resorting an Array or ArrayList. Behind the scenes, a sorted list uses two arrays: one to store key values, and one to store the data itself (or the object reference).

The most important aspect to understand about the SortedList class is that it orders items based on the key value, not the object itself. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required