SortDescription Structure
Defines the direction and the property name to be used as the criteria for sorting a collection.
Assembly: WindowsBase (in WindowsBase.dll)
XMLNS for XAML: Not mapped to an xmlns.
The SortDescription type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Direction | Gets or sets a value that indicates whether to sort in ascending or descending order. |
![]() | IsSealed | Gets a value that indicates whether this object is in an immutable state. |
![]() | PropertyName | Gets or sets the property name being used as the sorting criteria. |
| Name | Description | |
|---|---|---|
![]() | Equals | Compares the specified instance and the current instance of SortDescription for value equality. (Overrides ValueType::Equals(Object).) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Returns the hash code for this instance of SortDescription. (Overrides ValueType::GetHashCode().) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns the fully qualified type name of this instance. (Inherited from ValueType.) |
| Name | Description | |
|---|---|---|
![]() ![]() | Equality | Compares two SortDescription objects for value equality. |
![]() ![]() | Inequality | Compares two SortDescription objects for value inequality. |
This example describes how to sort data in a view.
The following example creates a simple ListBox and a Button:
<Window x:Class="ListBoxSort_snip.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ListBoxSort_snip" Height="300" Width="300"> <DockPanel> <ListBox Name="myListBox" DockPanel.Dock="Top"> <ListBoxItem>my</ListBoxItem> <!--Or you can set the content this way:--> <!--<ListBoxItem Content="my"/>--> <ListBoxItem>1</ListBoxItem> <ListBoxItem>Sort</ListBoxItem> <ListBoxItem>3</ListBoxItem> <ListBoxItem>ListBox</ListBoxItem> <ListBoxItem>2</ListBoxItem> </ListBox> <Button Click="OnClick" Width="30" Height="20" DockPanel.Dock="Top">Sort</Button> </DockPanel> </Window>
The Click event handler of the button contains logic to sort the items in the ListBox in the descending order. You can do this because adding items to a ListBox this way adds them to the ItemCollection of the ListBox, and ItemCollection derives from the CollectionView class. If you are binding your ListBox to a collection using the ItemsSource property, you can use the same technique to sort.
As long as you have a reference to the view object, you can use the same technique to sort the content of other collection views. For an example of how to obtain a view, see How to: Get the Default View of a Data Collection. For another example, see How to: Sort a GridView Column When a Header Is Clicked. For more information about views, see Binding to Collections in Data Binding Overview.
For an example of how to apply sorting logic in Extensible Application Markup Language (XAML), see How to: Sort and Group Data Using a View in XAML.
More Code
| How to: Sort and Group Data Using a View in XAML | This example shows how to create a view of a data collection in Extensible Application Markup Language (XAML). Views allow for the functionalities of grouping, sorting, filtering, and the notion of a current item. |
Windows 7, Windows Vista SP1 or later, Windows XP SP3, 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.
