Partager via


List<T>.Sort Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sorts the elements in the entire List<T> using the default comparer.

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub Sort
public void Sort()

Exceptions

Exception Condition
InvalidOperationException

The default comparer Comparer<T>.Default cannot find an implementation of the IComparable<T> generic interface or the IComparable interface for type T.

Remarks

This method uses the default comparer Comparer<T>.Default for type T to determine the order of list elements. The Comparer<T>.Default property checks whether type T implements the IComparable<T> generic interface and uses that implementation, if available. If not, Comparer<T>.Default checks whether type T implements the IComparable interface. If type T does not implement either interface, Comparer<T>.Default throws an InvalidOperationException.

This method uses Array.Sort, which uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n ^ 2) operation.

Examples

The following code example demonstrates the Sort() method overload and the BinarySearch(T) method overload. A List<T> of strings is created and populated with four strings, in no particular order. The list is displayed, sorted, and displayed again.

The BinarySearch(T) method overload is then used to search for two strings that are not in the list, and the Insert method is used to insert them. The return value of the BinarySearch method is negative in each case, because the strings are not in the list. Taking the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) of this negative number produces the index of the first element in the list that is larger than the search string, and inserting at this location preserves the sort order. The second search string is larger than any element in the list, so the insertion position is at the end of the list.

Imports System.Collections.Generic

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim dinosaurs As New List(Of String)

      dinosaurs.Add("Pachycephalosaurus")
      dinosaurs.Add("Amargasaurus")
      dinosaurs.Add("Mamenchisaurus")
      dinosaurs.Add("Deinonychus")

      outputBlock.Text &= vbCrLf
      For Each dinosaur As String In dinosaurs
         outputBlock.Text &= dinosaur & vbCrLf
      Next

      outputBlock.Text &= vbLf & "Sort" & vbCrLf
      dinosaurs.Sort()

      outputBlock.Text &= vbCrLf
      For Each dinosaur As String In dinosaurs
         outputBlock.Text &= dinosaur & vbCrLf
      Next

      outputBlock.Text &= vbLf & _
          "BinarySearch and Insert ""Coelophysis"":" & vbCrLf
      Dim index As Integer = dinosaurs.BinarySearch("Coelophysis")
      If index < 0 Then
         index = index Xor -1
         dinosaurs.Insert(index, "Coelophysis")
      End If

      outputBlock.Text &= vbCrLf
      For Each dinosaur As String In dinosaurs
         outputBlock.Text &= dinosaur & vbCrLf
      Next

      outputBlock.Text &= vbLf & _
          "BinarySearch and Insert ""Tyrannosaurus"":" & vbCrLf
      index = dinosaurs.BinarySearch("Tyrannosaurus")
      If index < 0 Then
         index = index Xor -1
         dinosaurs.Insert(index, "Tyrannosaurus")
      End If

      outputBlock.Text &= vbCrLf
      For Each dinosaur As String In dinosaurs
         outputBlock.Text &= dinosaur & vbCrLf
      Next

   End Sub
End Class

' This code example produces the following output:
'
'Pachycephalosaurus
'Amargasaurus
'Mamenchisaurus
'Deinonychus
'
'Sort
'
'Amargasaurus
'Deinonychus
'Mamenchisaurus
'Pachycephalosaurus
'
'BinarySearch and Insert "Coelophysis":
'
'Amargasaurus
'Coelophysis
'Deinonychus
'Mamenchisaurus
'Pachycephalosaurus
'
'BinarySearch and Insert "Tyrannosaurus":
'
'Amargasaurus
'Coelophysis
'Deinonychus
'Mamenchisaurus
'Pachycephalosaurus
'Tyrannosaurus
using System;
using System.Collections.Generic;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      List<string> dinosaurs = new List<string>();

      dinosaurs.Add("Pachycephalosaurus");
      dinosaurs.Add("Amargasaurus");
      dinosaurs.Add("Mamenchisaurus");
      dinosaurs.Add("Deinonychus");

      outputBlock.Text += "\n";
      foreach (string dinosaur in dinosaurs)
      {
         outputBlock.Text += dinosaur + "\n";
      }

      outputBlock.Text += "\nSort" + "\n";
      dinosaurs.Sort();

      outputBlock.Text += "\n";
      foreach (string dinosaur in dinosaurs)
      {
         outputBlock.Text += dinosaur + "\n";
      }

      outputBlock.Text += "\nBinarySearch and Insert \"Coelophysis\":" + "\n";
      int index = dinosaurs.BinarySearch("Coelophysis");
      if (index < 0)
      {
         dinosaurs.Insert(~index, "Coelophysis");
      }

      outputBlock.Text += "\n";
      foreach (string dinosaur in dinosaurs)
      {
         outputBlock.Text += dinosaur + "\n";
      }

      outputBlock.Text += "\nBinarySearch and Insert \"Tyrannosaurus\":" + "\n";
      index = dinosaurs.BinarySearch("Tyrannosaurus");
      if (index < 0)
      {
         dinosaurs.Insert(~index, "Tyrannosaurus");
      }

      outputBlock.Text += "\n";
      foreach (string dinosaur in dinosaurs)
      {
         outputBlock.Text += dinosaur + "\n";
      }
   }
}

/* This code example produces the following output:

Pachycephalosaurus
Amargasaurus
Mamenchisaurus
Deinonychus

Sort

Amargasaurus
Deinonychus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Coelophysis":

Amargasaurus
Coelophysis
Deinonychus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Tyrannosaurus":

Amargasaurus
Coelophysis
Deinonychus
Mamenchisaurus
Pachycephalosaurus
Tyrannosaurus
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.