The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

Sorting Basics

  • 3 min to read

XPO allows you to apply sorting both on server and client sides.

Sorting Data on the Server Side

Before persistent objects are retrieved from a data store, they can be sorted by specific data store fields. To accomplish this, use the properties listed in the following table.

Class

Property

XPView

ViewProperty.Sorting

XPCursor

XPCursor.Sorting

XPCollection

XPBaseCollection.Sorting.

When the XPBaseCollection.TopReturnedObjects and/or XPBaseCollection.SkipReturnedObjects filter settings are applied to XPCollection, the collection content is sorted on the server side.

The ViewProperty.Sorting property represents a sort order option for a specific data field, while the XPCursor.Sorting and XPBaseCollection.Sorting properties represent a collection of SortProperty objects that provide sorting settings for a data store. A SortProperty object provides the SortProperty.PropertyName and SortProperty.Direction properties that allow the data field and its sort order to be specified.

In the following example, the XPCollection retrieves the top five objects from a data store. Before these objects are retrieved, the data store is sorted against the 'Name' field.


using DevExpress.Xpo.DB;
using DevExpress.Xpo;

class Customer : XPObject {
    public string Name;
    public int Age;
}
...
// Create an XPCollection that retrieves Customer objects from a data store.
XPCollection collection = new XPCollection(typeof(Customer), null);

// Populate the Sorting collection. The values in the "Name" field will be sorted in Ascending order
// before the XPCollection retrieves the objects from a data store.
collection.Sorting.Add(new SortProperty("Name", DevExpress.Xpo.DB.SortingDirection.Ascending));

// Specify the maximum number of objects retrieved by the collection from a data store.
collection.TopReturnedObjects = 5;

The image below shows the result:

Sorting_DataStore

In the following example, the XPView is sorted on the server against the Age column.


// Create an XPView that retrieves Customer objects from a data store.
XPView view = new XPView(Session.DefaultSession, typeof(Customer));

// Populate the view's Properties collection.
view.Properties.AddRange(new ViewProperty[] {   
  new ViewProperty("Name", SortDirection.None, new OperandProperty("Name"), false, true),
  new ViewProperty("Age", SortDirection.Descending, new OperandProperty("Age"), false, true)});

The image below shows the result:

Sorting_XPView

Sorting Data on the Client Side

After persistent objects have been retrieved from a data store, you can sort them by specific data fields on the client. To accomplish this, use the properties listed in the following table.

Class

Property

XPView

XPView.Sorting

XPDataView

XPDataView.Sorting

XPCollection

XPBaseCollection.Sorting.

XPCollection contents are sorted on the client side, unless the XPBaseCollection.TopReturnedObjects and/or XPBaseCollection.SkipReturnedObjects filter settings are applied to the collection.

As with the server-side sort settings, the Sorting property represents a collection of SortProperty objects that allows you to specify sort settings for specific view columns and collection fields.

In the following example, the XPView fetches data from a data source and then sorts it by the Type column.


XPView view1 = new XPView(Session.DefaultSession, typeof(Issue), "Name; Type", null);
SortingCollection sortCollection = new SortingCollection();
sortCollection.Add(new SortProperty("Type", DevExpress.Xpo.DB.SortingDirection.Descending));
view1.Sorting = sortCollection;
view1.TopReturnedRecords = 2;