≡
×
Plus
Manual
Examples
Reference
Download
Blog
Community
Support
How to sort column by date yyyy-mm-dd
How to sort column by date yyyy-mm-dd
mmarini
Posts: 5
Questions: 0
Answers: 0
April 2011
edited April 2011
in
General
Hi all,
I need to sort by date a column in this format:
[code]
Replies
mmarini
Posts: 5
Questions: 0
Answers: 0
April 2011
edited April 2011
Solved, I will show you how :-)
I've called it: "Hidden title date sorting MySQL format (yyyy-mm-dd)"
Just my little contrib to the great page: http://datatables.net/plug-ins/sorting
[code]
jQuery.fn.dataTableExt.oSort['title-date-asc'] = function(a,b) {
var mysqlDatea = a.split('-');
var mysqlDateb = b.split('-');
var x = (mysqlDatea[2] + mysqlDatea[1] + mysqlDatea[0]) * 1;
var y = (mysqlDateb[2] + mysqlDateb[1] + mysqlDateb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['title-date-desc'] = function(a,b) {
var mysqlDatea = a.split('-');
var mysqlDateb = b.split('-');
var x = (mysqlDatea[2] + mysqlDatea[1] + mysqlDatea[0]) * 1;
var y = (mysqlDateb[2] + mysqlDateb[1] + mysqlDateb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
And in the JS code:
[code]
{ "sSortDataType": "title-date", "sType": "string", "aTargets": [ 0 ] }
[/code]
Regards
crea2k
Posts: 14
Questions: 0
Answers: 0
December 2011
Hi, maybe a stupid question, but where do I put each bit of code ?
This discussion has been closed.
Replies
I've called it: "Hidden title date sorting MySQL format (yyyy-mm-dd)"
Just my little contrib to the great page: http://datatables.net/plug-ins/sorting
[code]
jQuery.fn.dataTableExt.oSort['title-date-asc'] = function(a,b) {
var mysqlDatea = a.split('-');
var mysqlDateb = b.split('-');
var x = (mysqlDatea[2] + mysqlDatea[1] + mysqlDatea[0]) * 1;
var y = (mysqlDateb[2] + mysqlDateb[1] + mysqlDateb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['title-date-desc'] = function(a,b) {
var mysqlDatea = a.split('-');
var mysqlDateb = b.split('-');
var x = (mysqlDatea[2] + mysqlDatea[1] + mysqlDatea[0]) * 1;
var y = (mysqlDateb[2] + mysqlDateb[1] + mysqlDateb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
And in the JS code:
[code]
{ "sSortDataType": "title-date", "sType": "string", "aTargets": [ 0 ] }
[/code]
Regards