This documentation is archived and is not being maintained.

ListBox.Sort Method

Sorts the items in the ListBox.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

protected virtual void Sort()

Sort forces the collection to add each item back to itself if the Sorted property is true. Each item is then inserted into the correct position.

Notes to Inheritors

You can override this method in your derived class to provide your own sorting routine. Access the results of the overridden Sort method by setting the Sorted property to true. When adding items to a ListBox, it is more efficient to sort the items first and then add new items.

The following code example demonstrates using the Sort method. The example demonstrates inheriting from the ListBox class and overriding the Sort method in the derived class to perform a user-defined sort. To run this example paste the following code in an empty form.

// The following code example demonstrates using the ListBox.Sort method
// by inheriting from the ListBox class and overriding the Sort method.

using System.Drawing;
using System.Windows.Forms;

public class Form1:

	internal System.Windows.Forms.Button Button1;
	internal SortByLengthListBox sortingBox;
	public Form1() : base()
		this.Button1 = new System.Windows.Forms.Button();
		this.sortingBox = 
			new SortByLengthListBox();
		this.Button1.Location = new System.Drawing.Point(64, 16);
		this.Button1.Name = "Button1";
		this.Button1.Size = new System.Drawing.Size(176, 23);
		this.Button1.TabIndex = 0;
		this.Button1.Text = "Click me for list sorted by length";
		this.Button1.Click += new System.EventHandler(Button1_Click);
		this.sortingBox.Items.AddRange(new object[]{"System", 
			"System.Windows.Forms", "System.Xml", "System.Net", 
			"System.Drawing", "System.IO"});
		this.sortingBox.Location = 
			new System.Drawing.Point(72, 48);
		this.sortingBox.Size = 
			new System.Drawing.Size(120, 95);
		this.sortingBox.TabIndex = 1;
		this.ClientSize = new System.Drawing.Size(292, 266);
		this.Name = "Form1";
		this.Text = "Sort Example";
   	public static void Main()
		Application.Run(new Form1());

	private void Button1_Click(System.Object sender, System.EventArgs e)
		// Set the Sorted property to True to raise the overridden Sort
		// method.
		sortingBox.Sorted = true;

// This class inherits from ListBox and implements a different 
// sorting method. Sort will be called by setting the class's Sorted
// property to True.
public class SortByLengthListBox:

	public SortByLengthListBox() : base()

	// Overrides the parent class Sort to perform a simple
	// bubble sort on the length of the string contained in each item.
	protected override void Sort()
		if (Items.Count > 1)
			bool swapped;
				int counter = Items.Count - 1;
				swapped = false;
				while (counter > 0)
					// Compare the items' length. 
					if (Items[counter].ToString().Length  
						< Items[counter-1].ToString().Length)
						// Swap the items.
						object temp = Items[counter];
						Items[counter] = Items[counter-1];
						Items[counter-1] = temp;
						swapped = true;
					// Decrement the counter.
					counter -= 1;

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.