Using Javascript to sort a table programmatically

I need to modify the sort order of a table programmatically at runtime.

More precisely, I want to invert the sort order or a table when I click on date/time type header, so I see the most recent at top instead of the default which forces me to click twice every time. I also want to implement Ctrl+click to invert the default sort. Or press a function key (F2, F3, F4, etc) to apply preset sorts on columns. That kind of stuff.

I’ve had some success in my KTL library, but it’s not always behaving as I like. And it takes a long time to apply (several seconds) and I need to refresh the view twice for some reason.

If you want to see what I have now, go to this file: Knack-Toolkit-Library/KTL_KnackApp_ACB.js at master · cortexrd/Knack-Toolkit-Library (

and search this function: modifyTableSort

These lines are at the center of the feature:

Knack.views[viewId].model.view.source.sort[0].order = invert ? 'asc' : 'desc';

But I’m not sure I have the right technique. There could be a clever hidden function somewhere that does just that.

Any help would be appreciated,
Normand D.