Export (0) Print
Expand All

XPathExpression.AddSort Method (Object, IComparer)

When overridden in a derived class, sorts the nodes selected by the XPath expression according to the specified IComparer object.

Namespace:  System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)

public abstract void AddSort(
	Object expr,
	IComparer comparer
)

Parameters

expr
Type: System.Object

An object representing the sort key. This can be the string value of the node or an XPathExpression object with a compiled XPath expression.

comparer
Type: System.Collections.IComparer

An IComparer object that provides the specific data type comparisons for comparing two objects for equivalence.

ExceptionCondition
XPathException

The XPathExpression or sort key includes a prefix and either an XmlNamespaceManager is not provided, or the prefix cannot be found in the supplied XmlNamespaceManager.

The AddSort method enables users to sort objects by their data type instead of by string or number. The IComparer object provides an implementation of the Compare method that supports sorting on user-defined classes.

In the following example, the books are sorted by ISBN number, where isbn is an object that implements the IComparer interface.

XPathExpression expression = navigator.Compile("bookstore/book");
ISBN isbn = new ISBN();
expression.AddSort("@ISBN", (IComparer)isbn);

The following are important notes to consider when using the AddSort method.

  • The order in which the sorts are added provides the sort key order.

  • If the XPathExpression or the sort key requires namespace resolution, you must use the SetContext method to provide an XmlNamespaceManager for namespace resolution.

  • If the XPathExpression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. If your XML includes a default namespace, you must still use the SetContext method and provide an XmlNamespaceManager that contains a prefix and namespace URI to handle the default namespace.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft