how to set a datatable with a sorted column by default

I have a databable which uses ajax to load data from server. The data is not sorted at the server. I want to get datatable sorted on one column by default when it’s displayed.

Say I have the “siteId” property in my data model, and want the datatable to be sorted on the property when it’s displayed.

So I add the onAfterLoad function and try the following in the function:
// this.sort(“siteId”);
this.sort("#siteId#");
this.markSorting(“siteId”, “asc”);

Yeah, I get the sorted mark on the column, but the data is not actually sorted.
What else do I need to add to get this to work?

Hi,

Could you please share a snippet with the issue? As far as I can see, everything works smoothly: https://webix.com/snippet/797fd2fc

After seeing your example, I tried it on my own app again. It works fine on columns with default sorting type, like “string”.

But I have an IPV4 address column with a custom “ipv4” sorting type. It’s sorted as expected if I click on the column title. But it does not sort by the sort() call in the onAfterLoad event.

Could you please share a snippet of your code? I didn’t succeed in repeating the issue even with custom sorting methods.

Here’s an example with a custom sorting type: https://webix.com/snippet/a8e37fe1

And with a custom sorting function: https://webix.com/snippet/ff844336

It’s my own fault, I guess.
I assume if I do not specify the sorting type in the sort call, the sorting type specified in the column definition will be used as the default. But no, the default sorting type is always “string”, irregardless of the column definition.
After I specify the sorting type in the sort call, it works as expected.

But, hey, I’m calling this.sort() on the datatable instance, and I have sorting type defined on the column, why does not the sort() method get sorting type from the column definition as the default?

Default sorting works wrongly. Try to click twice and ull see a different order
https://snippet.webix.com/s0y2cbam

https://webix.com/snippet/ff844336
ur example also works wrong

this why i’m here, looking for a solution. i expected that my code works wrongly, cause by it complexity is not even close with any examples i ve seen here. was surprised i’ve reproduced.

in real sort compare by several bool field with their own priority (director, active, deleted, partner - in one field “active”)

This result is expected, since the initial sorting is done by a predefined sorting function, whereas table columns use a custom sorting function (sorting by length). Therefore, the sorting logic differs between both cases, leading to the observed result. If you want a consistent outcome, the sorting logic must be identical in both cases.

In actuality, it should look like this if you want to use a custom sorting function:

See sort - The sort method documentation page: sorting DataTable rows. Webix Docs for reference.