sorting field according to months description

sorting field according to months description

usmanusman Posts: 2Questions: 0Answers: 0
edited October 2010 in General
How to sort the fields according to moths description . i try using the following code
[code]
var monthsArray=new Array("Gennaio","febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","novembre","dicembre");
jQuery.fn.dataTableExt.oSort['string-case-asc'] = function(x,y) {
alert( "asc");
var indexOfx = monthsArray.indexOf(x);
var indexOfy = monthsArray.indexOf(y);
return ((indexOfx < indexOfy) ? -1 : ((indexOfx > indexOfy) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['string-case-desc'] = function(x,y) {
alert( "desc");
var indexOfx = monthsArray.indexOf(x);
var indexOfy = monthsArray.indexOf(y);
return ((indexOfx < indexOfy) ? 1 : ((indexOfx > indexOfy) ? -1 : 0));
};

$(document).ready(function() {
$('#example').dataTable({
"bPaginate": false,
"bLengthChange": false,
"bFilter": false,
"bSort": true,
"bInfo": false,
"aoColumnDefs": [
{ "sType": 'string-case', "aTargets": [ 0 ] }
],
"bAutoWidth": false
});
} );
[code]

but not working properly any ideas ....

Replies

  • mstrandmstrand Posts: 24Questions: 0Answers: 0
    Have you seen the Week Day sorting plug-in here:

    http://www.datatables.net/plug-ins/sorting

    This might be easily changed for your need.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It's probably worth noting that Array.indexOf appears not to be very cross browser compliant, so that might be part of the problem - using jQuery's $.inArray might be a good idea for this - but other than that I don't see anything wrong with the code on a quick scan. Do you get any JS errors? One other thought that comes to mind, you've got mixed case month names in your array - which won't help. I'd say almost certainly that Array.indexOf will not be case-insensitive - so that also could be the issue.

    Allan
This discussion has been closed.