Look at this dojo
"fld3" is a special field based on array, and the "sort" is configured to sort sort by array.length.
That's why we used the column attribute sortable.compare (function).
It works fine when we click on the grid header, from single or multiple sortable configuration.
But, when we sort it from code, using the kgrid.dataSource.sort(...) method, the custom compare function is not executed.
There's a grid databinding, The up-down arrow on top of the grid column is moving according to the "direction", but the data doesn't move.
In the dojo, sort on fld3 column, and switch the "direction" attribute in the "json textbox" between "asc" and "desc", and click the "set" button.
You'll see the arrow moving, but not the data.
And the fld4 timestamp column is not changing, proof that the compare function is not executed.
5 Answers, 1 is accepted
Hmmm after a few more readings, I think that I know your response... let me guess.
The "Sorting.compare" option is send to the datasource from the grid when the user click the columns, but is not used anywhere else. So, I need to send this same parameter to the datasource directly...
Ok, but if I need to set it only once and make it easy?
I have 2 suggestions for you.
1. add a sort "method" to the grid, that will call the sort of the data Source with the column configuration.
2. Make it very global and reusable for all component:
Allow us to set it directly in the "model", like you can see in this code sample:
Your observations for the sort.compare function are correct. When calling the dataSource sort method there is no compare function specified and since the underlying field is array the DataSource does not know how to order the data.
With that said, you can pass a compare function to the sort method and it will be used for sorting the data. In the current setup I suggest defining a function that will be used for comparing the records. Then reference that function in the columns.sortable.compare and sort.compare options.
For your convenience I have updated the dojo sample to illustrate the approach:
Give it a try and let me know how it works for you.
Thank you for your suggestion, that's exactly what we did finally... we create a "sort" function, and before sending it to the datasource, we loop the columns and add the "custom" compare functions.
That said, I still think that you should allow us to configure the "customCompare" function in the datasource "model / schema", to allow it to be inherited to all datasource needs...
Sometimes, we take the grid datasource and I send it to the scheduler, or any other kendo ui component.
Or we reorder the data before sending it to a workbook for export.
Having the datasource knowing all the customcompare configuration for each field would be great!
For those who read this thread with the same issue...
Please note that if you're using a custom compare function for grouping, you also need to do the same "hack" if you apply grouping directly in the dataSource, and not from a field drag-drop on the grid.
Also, if you configure the grid with the dataSource + grouping, you need to set the custom compare (for sort and grouping ) at both places, in "columns" and in dataSource.sort / dataSource,groups.
If telerik allow us to configure customcompare functions for sorting and grouping in the kendo schema model, it will always work as expected without any "hack", and will also work in all others UI components, not just in grid.
Thank you for the feedback.
When grouping is applied sorting a column will apply the order inside each of the Groups. In case there is a custom sort.compare function it will be used to apply the sorting.
If you would like to customize the order of the groups themselves a group.compare function will be necessary