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.

Exception Condition
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
Available since 1.1
Silverlight
Available since 4.0
Return to top
Show: