I have a visualforce page that I want to sort its columns by a datetime field which I do in the SOQL query via the controller using the ORDER by myDateTime__c DESC. However I also need to use javascript/jquery to make the other columns in the table sortable in case the user wishes to do so. Problem is when I disable the javascript sort, results get ordered by the dateTime field as desired but other columns are not sortable. when I enable it, then results lose their order but other columns are then sortable. Anybody know what I am doing wrong based on the code snippet below?

        function bindRowEvents() {
            var $comTable = $j("#com-table")
            cDataTable = $comTable.dataTable( {
                "bDestroy" : true,
                "bSort": true,
                "sScrollY": "400px",
                "bAutoWidth": true,
                "sProcessing": "Processing...",
                "aoColumnDefs": [ 
                //{ "bSortable": true,"aTargets":[] },
                //{ "aDataSort": [ 0 ], "aTargets": [ 0 ] }
                { "asSorting": [ "desc" ], "aTargets": [ 0 ] }
                "bPaginate": false,
                "iDisplayLength": 4,
                "bLengthChange": false,
                "oLanguage": {
                        "sEmptyTable": "No matching Organizations found."

4 Answers 4


The javascript is running after the results have been returned by your query, so the order of the query is lost.

You can specify the column that you want initially sorted in the javascript e.g.

{"aaSorting":[[7, 'desc']]}

This will sort by the 8th column

  • I tried it with the first column which works but then the other columns are not sortable. this is what I ran "aoColumnDefs": [ //{ "bSortable": true,"aTargets":[] }, //{ "aDataSort": [ 0 ], "aTargets": [ 0 ] } //{ "asSorting": [ "desc" ], "aTargets": [ 0 ] } { "aaSorting":[[0, 'desc']]} ], When I uncomment out the bsortable line, then I am back in to other columns sorting but no order
    – Mike M
    Commented Jul 9, 2013 at 13:53
  • You have a lot of options in there, and I'm not familiar with all the datatable parameters. I can tell you that I have invoked datatable like this .dataTable({"aaSorting":[[7, 'desc']]}); and it was sortable on all columns plus it defaulted to sort by column 8. I would suggest you try it with this simple parameter set, then add the other parameters back in one by one. Hope that helps!
    – Doug B
    Commented Jul 9, 2013 at 14:00
  • Kept it to a minimum but still no luck. Not sure if splitting the function and having a separate click function just for the columns will do.
    – Mike M
    Commented Jul 9, 2013 at 18:13

Ok this is how I ended up solving it. Problem was that I was calling the aaSorting[] function within the aocolumnDefs[] function which shouldn't be the case because they are all on the same level, as in one is not a child of the other. Below is a snippet of the fix: "bSort": true, "sScrollY": "400px", "bAutoWidth": true, "sProcessing": "Processing...", "aoColumnDefs": [ { "bSortable": true,"aTargets":[]} ], "aaSorting":[], Didn't have to even have to specify columns to sort by.

  • Glad you got it sorted in the end!
    – Doug B
    Commented Jul 10, 2013 at 8:13

Could you just handle the default sorting with the javascript as well? Set the default sorting with:

 aaSorting: [[0, 'desc']],

Set additional colummns to be sortable with:

aoColumnDefs: [
{ "aTargets": [ 0 ], "bSortable": true }

Additional columns to be sortable would be as follows:

 aoColumnDefs: [
            { "aTargets": [ 0 ], "bSortable": true },
            { "aTargets": [ 1 ], "bSortable": true }

  • I tried that approach which sorts as desired but columns are not clickable. I am wondering if I should just split the function into two and just have an on click function just for the table columns. they get sorted only when user clicks on a column other wise they use the default sorting from the controller.
    – Mike M
    Commented Jul 9, 2013 at 18:11

@Mike M Your approach of having 2 functions one for the default sorting and one for the onclick of the column would make it easier to debug and maintenance instead of doing all in once where it makes it more complicated now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .