Sorting on weekdays
Sorting on weekdays
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.
(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.
This discussion has been closed.
Replies
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
Allan