Sorting on weekdays

Sorting on weekdays

twistahtwistah Posts: 5Questions: 0Answers: 0
edited April 2009 in General
I dont know if this is the right place, but I just wanted to share a bit of sorting code that can be added to your DataTables initialization code to make a column that contains the names of weekdays to be sorted correctly.

(can I use bb-code in this posting?)

[code]
jQuery.fn.dataTableExt.aTypes.push(
function ( sData ){
var sValidStrings = 'monday,tuesday,wednesday,thursday,friday,saturday,sunday';

if (sValidStrings.indexOf(sData.toLowerCase()) >= 0){
return 'weekdays-sort';
}

return null;
}
);

var weekdays = new Array();
weekdays['monday'] = 1;
weekdays['tuesday'] = 2;
weekdays['wednesday'] = 3;
weekdays['thursday'] = 4;
weekdays['friday'] = 5;
weekdays['saturday'] = 6;
weekdays['sunday'] = 7;

jQuery.fn.dataTableExt.oSort['weekdays-sort-asc'] = function(a,b) {
a = a.toLowerCase();
b = b.toLowerCase();
return ((weekdays[a] < weekdays[b]) ? -1 : ((weekdays[a] > weekdays[b]) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['weekdays-sort-desc'] = function(a,b) {
a = a.toLowerCase();
b = b.toLowerCase();
return ((weekdays[a] < weekdays[b]) ? 1 : ((weekdays[a] > weekdays[b]) ? -1 : 0));
};
[/code]

This code is probably not optimal, but it works for me.

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    HI twistah,

    That's fantastic! Thanks very much for sharing your code. Do you mind if I put it on the DataTables plug-ins page? You you have a web-site you'd like me to link to with the credit for this function set.

    Regards,
    Allan
  • ministrypixelministrypixel Posts: 12Questions: 0Answers: 0
    Where in the code setup of Datatables do I plug this in at? Do I need to do anything special to my headers?
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Top of the sorting plug-ins page shows you you can use the plug-ins: http://datatables.net/plug-ins/sorting#how_to_type . There is also a working example: http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html

    Allan
This discussion has been closed.