M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

ISortable and Sorting

This tutorial focuses on the "ISortable" interface provided by M# framework and explains how you can implement persistent ordering of an entity within the database, by using this interface and the public methods exposed by the Sorter class. Also we will cover the IComparable interface, as we often need a way of sorting custom entities, other than their standard order.

ISortable Interface

This interface is defined under "MSharp.Framework.Services" and contains only one int type property definition. This property must be implemented in the entity type in order to perform sorting on the collection of an entity instance.

ISortable Implementation

In order to have a persistent order within the database the entity must implement ISortable interface. Let’s take an example of an entity type Tutorial, for which we want to persist the order of each instance so that they can be displayed in the order we specify.

We have created an entity type Tutorial with only one property "Title", as shown in the screenshot below: (Please read Entities, page, Modules tutorial for more information on creating entities and Properties for managing properties of an entity in M#)

In order to implement ISortable interface we create a property "Order" of type int. M# instantly underlines "Sort Property" attribute as soon as a mandatory property "Order" of int type is created, as shown below.

This "Sort Property" should not be confused with ISortable interface. When "Sort Property" attribute is specified, M# implements "IComparable" interface defined in .NET framework and utilizes the property specified as "Sort Property" in the implementation of "CompareTo" method defined in "IComparable" interface, as shown below:

The sorting based on "IComparable" implementation happens after records are fetched in memory, but for persisting and managing sort order of entity instances, ISortable interface must be implemented using a separate M# attribute "Sortable" on the entity, as shown below:

M# also generates "OnSaving" event of the entity to populate the "Order" property for a new entity instances being saved, which maintains the sort order sequence ( Database.Save tutorial explains entity events in more detail).

Sorter Class

M# framework contains very useful static methods implemented in Sorter class to perform sorting action on an entity instance of interface type ISortable. The screenshots below show the definitions:


In order to perform sorting, we just need to pass an ISortable type instance to the methods "MoveUp, MoveDown, MoveFirst and MoveLast". Each of the aforementioned methods return "True" if the sorting is successful.

M# also provides "Move Up" and "Move Down" actions for "List Buttons", which can be used to manage sort order of records displayed in a list (Please read tutorial List Button for more information)