Adding custom sorting (substring of a column)

I currently sort grid data in php using something like this

$grid->sort("Num1", "ASC"); $grid->sort("Num2", "ASC");

I would like to add a rule on a third value before these two sorting columns, the value would be a substring (actually an interger value of a substring) of a third field. This third value should be the first value the grid is sorted (before Num1 and Num2).

Now I have seen:

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

But what can I put on the right hand side of the “=” ? PHP code ? intval(substr()) ?

How do I defined this rule to be the integer value of a substring (or two substrings)? How can I say it has to be ascending or descending?

If I use a custom_sort, should I move my two sorting commands (on Num1, Num2) inside that function ?

Any documentation on what may be set in the sorting rules, we know LENGTH() can be used… Anything else ?

Each sorting rule is an object with two parameters name and direction, which will be translated to the SQL code like next

"ORDER BY $name $dir"

So you can set any valid SQL code for the $name and $dir parameters.

Thank you Stanislav, I will try that out.