21

Here's the page:

http://csuvscu.com/

I need to sort by the Date Column, right now it needs to read Nov 6, Nov 5 and lastly Oct 7.

How do I do this?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Snow_Mac
  • 5,727
  • 17
  • 54
  • 80
  • 3
    I would recommend adding some applicable code to your question. Yes a person could go to the page you've linked and view the source, but the idea is that as long as you provide the information needed here, the code will still be available even if your site were to go down sometime in the future. – Kiley Naro Oct 24 '11 at 15:37
  • Take a look at the following [post](http://www.codeunit.co.za/2010/04/04/jquery-datatables-how-to-sort-by-a-specific-column-on-load/) – Emmanuel N Oct 24 '11 at 15:44
  • @KileyNaro makes a good point about the site going down, I would still keep the link - it was nice for me not to have to recreate a test on my local machine for something like this -- did it all in Chrome javascript console :) – shaheenery Oct 24 '11 at 15:48
  • 5
    that moment has arrived... –  Feb 17 '16 at 21:02
  • 2
    This question is now useless. The link to the problem specification is dead. – Jean-François Corbett Sep 12 '18 at 09:43

7 Answers7

58

Your Current Code:

$('table').dataTable({
    // display everything
    "iDisplayLength": -1
});

What you could do:

oTable = $('table').dataTable({
    // display everything
    "iDisplayLength": -1
});

oTable.fnSort( [ [0,'desc'] ] ); // Sort by first column descending

But as pointed out in the comment below, this may be a cleaner method:

$('table').dataTable({
    // display everything
    "iDisplayLength": -1,
    "aaSorting": [[ 0, "desc" ]] // Sort by first column descending
});
Serrano
  • 1,457
  • 20
  • 17
shaheenery
  • 8,457
  • 2
  • 17
  • 14
  • 1
    You're welcome...as @emmanual N linked_to above, you could aways use this form which is a cleaner in my opinion `$('table').dataTable({ "iDisplayLength": -1 "aaSorting": [[ 0, "desc" ]] });` – shaheenery Oct 24 '11 at 16:05
  • when am trying this in Java method to set options, am missing something. Could you please add it for java side too ? – sandeep kale Jul 09 '15 at 17:04
13

DataTables uses Alphabetical order as the default sorting method. This is actually what happens here.

There are two solution:

  • Define your own date sorting method
  • Sort the table using an hidden column containing the date in Unix Timestamp (seconds elapsed since 1st January 1970).

If you want your users to be able to sort the column by themselves you might use the first solution.

--------------- First Solution:

We need to tell the DataTable plugin what to do with our columns. You'll need to use the "aoColumns" property:

$('table').dataTable({
    // display everything
    "iDisplayLength": -1,
    "aoColumns":[
        {"sType": "shaheenery-date"},
        {"bSortable": true},
        {"bSortable": true},
        {"bSortable": true},
        {"bSortable": true}
    ]
});

Then define the "shaheenery-date-asc" and "shaheenery-date-desc" sorting method. You also need a function "getDate" that translate the date in numeric format:

function getDate(a){
        // This is an example:
        var a = "Sunday November 6, 2011";
        // your code =)
        // ...
        // ...
        // You should output the result as YYYYMMDD
        // With :
        //   - YYYY : Year
        //   - MM : Month
        //   - DD : Day
        //
        // Here the result would be:
        var x = 20111106
        return x;
}

jQuery.fn.dataTableExt.oSort['shaheenery-date-asc'] = function(a, b) {      
        var x = getDate(a);
        var y = getDate(b);
        var z = ((x < y) ? -1 : ((x > y) ? 1 : 0));
        return z;
};

jQuery.fn.dataTableExt.oSort['shaheenery-date-desc'] = function(a, b) {
        var x = getDate(a);
        var y = getDate(b);
        var z = ((x < y) ? 1 : ((x > y) ? -1 : 0));
        return z;
    };

--------------- Second Solution:

We are going to use the "aoColumns" property as well. This time we tell DataTable to hide the last column which will contains the date in Unix Timestamp. We also need to define this column as the default one for sorting with "aaSorting":

$('table').dataTable({
    // display everything
    "iDisplayLength": -1,
    "aaSorting": [[ 5, "desc" ]],
    "aoColumns":[
        {"bSortable": false},
        {"bSortable": true},
        {"bSortable": true},
        {"bSortable": true},
        {"bVisible": false}
    ]
});
paic_citron
  • 301
  • 2
  • 4
2
oTable = $('#DataTables_Table_0').dataTable({   //table id -->DataTables_Table_0

    iVote: -1,  //field name 
    "bRetrieve":true

});

 oTable.fnSort( [ [1,'desc'] ] );   // Sort by second column descending
Divyesh
  • 389
  • 2
  • 12
2

With the latest version of Data Tables you can sort by column index

var data_table = $('#data-table').DataTable();
data_table.order( [7,'desc'] ).draw();

Hope this helps.

Darren Hall
  • 920
  • 8
  • 13
1
 $('#id').dataTable( {
     "bSort": true,
     "aoColumnDefs": [{ 
         'bSortable': false, 
         'aTargets': [ 1 ] }
      ]
});
1

The existing answers are using legacy DataTables syntax. Versions 1.10+ should use the following syntax:

$('table').dataTable({
    "pageLength": -1,  //display all records
    "order": [[ 0, "desc" ]] // Sort by first column descending
});

Reference:

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
-3

$('#id').dataTable( {

""aaSorting": [[ "0", "<"desc" or asc>"]]

});