# dhtmlxconnector:sorting [DHX documentation]

DHTMLX Docs & Samples Explorer

Index
dhtmlxconnector
Usage Guide

## Sorting

While all connectors can sort data by url manipulation, only Grid and TreeGrid has native GUI for sorting, so below info most actual for those two components.

To make grid sort content with connectors you need to use “connector” as sorting type while grid initialization. For example:

        myGrid.setColSorting("connector,str,na);

Here the first column will be sorted on server with connectors, the second as string on client side, the third column will not be sortable.

If you need to customize the way of sorting you can use “beforeSort” server side event. Event doesn't allow to write custom sorting logic , but you can affect SORT BY clause of generated SQL request. Event receives SortInterface Object as parameter

Define default sorting

       function custom_sort($sorted_by){
              //SORT BY some_field ASC
              if (!sizeof($sorted_by->rules))
                   $sorted_by->add("some_field","ASC");
       }
       $conn->event->attach("beforeSort","custom_sort");

Default sorting by two fields

       function custom_sort($sorted_by){
              //SORT BY some_field ASC, some_other ASC
              if (!sizeof($sorted_by->rules)){
                   $sorted_by->add("some_field","ASC");
                   $sorted_by->add("some_other","ASC");
              }
       }
       $conn->event->attach("beforeSort","custom_sort");

Custom sorting rule

       function custom_sort($sorted_by){
              // SORT BY LENGTH(some_field)
              $sorted_by->rules[0]["name"]="LENGTH(some_field)";
       }
       $conn->event->attach("beforeSort","custom_sort");