From 1d3707ad4385b87c614ec61a203af017bcd07fd2 Mon Sep 17 00:00:00 2001 From: Jeromy French Date: Fri, 17 May 2013 16:14:13 -0400 Subject: [PATCH] Addresses issue #17. --- sorting/date-dd-MMM-yyyy.js | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 sorting/date-dd-MMM-yyyy.js diff --git a/sorting/date-dd-MMM-yyyy.js b/sorting/date-dd-MMM-yyyy.js new file mode 100644 index 0000000..6e6810f --- /dev/null +++ b/sorting/date-dd-MMM-yyyy.js @@ -0,0 +1,47 @@ +/* + * Adds a new sorting option to dataTables called date-dd-mmm-yyyy. Also + * includes a type detection plug-in. Matches and sorts date strings in + * the format: (dd/mmm/yyyy. For example: + * + * + * @name Date dd-mmm-yyyy + * @anchor date-dd-mmm-yyyy + * @author Jeromy French + */ + +var customDateDDMMMYYYYToOrd = function (date) { + "use strict"; //let's avoid tom-foolery in this function + // Convert to a number YYYYMMDD which we can use to order + var dateParts = date.split(/-/); + return (dateParts[2] * 10000) + ($.inArray(dateParts[1].toUpperCase(), ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]) * 100) + dateParts[0]; +}; + +// This will help DataTables magic detect the "dd-MMM-yyyy" format; Unshift so that it's the first data type (so it takes priority over existing) +jQuery.fn.dataTableExt.aTypes.unshift( + function (sData) { + "use strict"; //let's avoid tom-foolery in this function + if (/^([0-2]?\d|3[0-1])-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-\d{4}/i.test(sData)) { + return 'date-dd-mmm-yyyy'; + } + return null; + } +); + +// define the sorts +jQuery.fn.dataTableExt.oSort['date-dd-mmm-yyyy-asc'] = function (a, b) { + "use strict"; //let's avoid tom-foolery in this function + var ordA = customDateDDMMMYYYYToOrd(a), + ordB = customDateDDMMMYYYYToOrd(b); + return (ordA < ordB) ? -1 : ((ordA > ordB) ? 1 : 0); +}; + +jQuery.fn.dataTableExt.oSort['date-dd-mmm-yyyy-desc'] = function (a, b) { + "use strict"; //let's avoid tom-foolery in this function + var ordA = customDateDDMMMYYYYToOrd(a), + ordB = customDateDDMMMYYYYToOrd(b); + return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0); +}; \ No newline at end of file