Sorting

11 Aug 20172 minutes to read

Default

Sorting is basic query in the DataManager. It enables you to view the items or records in ascending or descending order based on a particular field and sorting direction. The query parameter of the DataManager enables you to retrieve the data in the sorted fashion and thus utilizing the resultant data obtained.

  • CSHTML
  • @Html.EJ().DataManager("FlatData").URL("http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/").Adaptor(AdaptorType.ODataAdaptor).CrossDomain(true)
    
    	@(Html.EJ().Grid<MVCdoc.OrdersView>("FlatGrid")
    
    		.DataManagerID("FlatData")
    		.Query("new ej.Query().sortBy('CustomerID', ej.sortOrder.Ascending, false).take(5)")
    		.Columns(col =>
    		{
    			col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
    			col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
    			col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Width(75).Add();
    			col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add();
    			col.Field("ShipCity").HeaderText("Ship City").Width(110).Add();
    		})
    
    	)

    Result of the above code example is illustrated as follows.

    Sorting

    SortByDesc

    The sortByDesc query of the DataManager is used to sort the specified field in descending order, by default. You can use the following code example for sortByDesc query.

  • CSHTML
  • @Html.EJ().DataManager("FlatData").URL("http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/").Adaptor(AdaptorType.ODataAdaptor).CrossDomain(true)
    
    	@(Html.EJ().Grid<MVCdoc.OrdersView>("FlatGrid")
    		.DataManagerID("FlatData")
    		.Query("new ej.Query().sortByDesc('EmployeeID').take(5)")
    		.Columns(col =>
    		{
    
    			col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
    			col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
    			col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Width(75).Add();
    			col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add();
    			col.Field("ShipCity").HeaderText("Ship City").Width(110).Add();
    		})	
    	)

    Result of the above code example is illustrated as follows.

    Sort in descending order

    Dynamic sorting

    The table can be dynamically sorted by using an external button click event. The value of the column to be sorted can be obtained by the sortBy query and thus the sorted data is retrieved and bounded to the table. The following code example illustrates you to dynamically sort the data source.

  • CSHTML
  • @Html.EJ().DataManager("FlatData").URL("http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/").Adaptor(AdaptorType.ODataAdaptor).CrossDomain(true)
    
    	@(Html.EJ().Grid<MVCdoc.OrdersView>("FlatGrid")
    		.DataManagerID("FlatData")
    		.Query("new ej.Query().sortByDesc('EmployeeID').take(5)")
    		.Columns(col =>
    		{
    			col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
    			col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
    			col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Width(75).Add();
    			col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add();
    			col.Field("ShipCity").HeaderText("Ship City").Width(110).Add();
    		})	
    	)
    
    	<input id="field" type="text" placeholder="fieldName" />
    
    	@Html.EJ().Button("submit").Text("Sort").ClientSideEvents(e => { e.Click("onClick"); })
    
    	<script type="text/javascript" class="jsScript">
    
            function onClick(e) {
                var field = $("#field").val();
                var obj = $("#FlatGrid").ejGrid("instance")
                var query = ej.Query().sortByDesc(field).take(5)
                var dataManagerObj = window.FlatData.executeQuery(query).done(function (e1) {
                    obj.dataSource(e1.result);
                })
            }
    
        </script>

    Result of above code example is illustrated as follows.

    Dynamic sorting

    Multi sorting

    Multi sorting is a special technique, where you can sort multiple fields by adding multiple sorting queries to the DataManager.

  • CSHTML
  • @Html.EJ().DataManager("FlatData").URL("http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/").Adaptor(AdaptorType.ODataAdaptor).CrossDomain(true)
    
    	@(Html.EJ().Grid<MVCdoc.OrdersView>("FlatGrid")
    		.DataManagerID("FlatData")
    		.Query("new ej.Query().sortByDesc('CustomerID').sortBy('EmployeeID', 'ascending').take(5)")
    		.Columns(col =>
    		{
    			col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
    			col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
    			col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Width(75).Add();
    			col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add();
    			col.Field("ShipCity").HeaderText("Ship City").Width(110).Add();
    		})	
    
    	)

    Result of above code example is illustrated as follows.

    Multi sorting