From 0bba914d8bc4d5c9217fd64f08fb760773aa6219 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Thu, 31 May 2012 11:01:31 +0100 Subject: [PATCH] Add sorting methods from the DataTables sorting plug-ins page Code updated to use $.extend - a bit cleaner --- sorting/.DS_Store | Bin 0 -> 6148 bytes sorting/alt-string.js | 23 ++++++++ sorting/anti-the.js | 24 ++++++++ sorting/currency.js | 31 ++++++++++ sorting/custom-data-source/dom-checkbox.js | 15 +++++ sorting/custom-data-source/dom-select.js | 15 +++++ sorting/custom-data-source/dom-text.js | 15 +++++ sorting/date-eu.js | 60 +++++++++++++++++++ sorting/date-euro.js | 54 +++++++++++++++++ sorting/date-uk.js | 32 ++++++++++ sorting/enum.js | 38 ++++++++++++ sorting/file-size.js | 41 +++++++++++++ sorting/ip-address.js | 62 ++++++++++++++++++++ sorting/monthYear.js | 22 +++++++ sorting/natural.js | 65 +++++++++++++++++++++ sorting/num-html.js | 29 +++++++++ sorting/numeric-comma.js | 28 +++++++++ sorting/percent.js | 25 ++++++++ sorting/persian.js | 50 ++++++++++++++++ sorting/scientific.js | 22 +++++++ sorting/signed-num.js | 21 +++++++ sorting/title-numeric.js | 31 ++++++++++ sorting/title-string.js | 24 ++++++++ 23 files changed, 727 insertions(+) create mode 100644 sorting/.DS_Store create mode 100644 sorting/alt-string.js create mode 100644 sorting/anti-the.js create mode 100644 sorting/currency.js create mode 100644 sorting/custom-data-source/dom-checkbox.js create mode 100644 sorting/custom-data-source/dom-select.js create mode 100644 sorting/custom-data-source/dom-text.js create mode 100644 sorting/date-eu.js create mode 100644 sorting/date-euro.js create mode 100644 sorting/date-uk.js create mode 100644 sorting/enum.js create mode 100644 sorting/file-size.js create mode 100644 sorting/ip-address.js create mode 100644 sorting/monthYear.js create mode 100644 sorting/natural.js create mode 100644 sorting/num-html.js create mode 100644 sorting/numeric-comma.js create mode 100644 sorting/percent.js create mode 100644 sorting/persian.js create mode 100644 sorting/scientific.js create mode 100644 sorting/signed-num.js create mode 100644 sorting/title-numeric.js create mode 100644 sorting/title-string.js diff --git a/sorting/.DS_Store b/sorting/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0Jumpy + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "alt-string-asc": function( a, b ) { + var x = a.match(/alt="(.*?)"/)[1].toLowerCase(); + var y = b.match(/alt="(.*?)"/)[1].toLowerCase(); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "alt-string-desc": function(a,b) { + var x = a.match(/alt="(.*?)"/)[1].toLowerCase(); + var y = b.match(/alt="(.*?)"/)[1].toLowerCase(); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/anti-the.js b/sorting/anti-the.js new file mode 100644 index 0000000..b95059e --- /dev/null +++ b/sorting/anti-the.js @@ -0,0 +1,24 @@ +/** + * Often a list of data which has titles in it (books, albums etc) will have + * the word "the" at the start of some individual titles, which you don't want + * to include in your sorting order. This plug-in will strip the word "the" + * from the start of a string and sort on what is left. + * @name Anti-"the" + * @author Allan Jardine + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "anti-the-asc": function ( a, b ) { + var x = a.replace(/^the /i, ""); + var y = b.replace(/^the /i, ""); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "anti-the-desc": function ( a, b ) { + var x = a.replace(/^the /i, ""); + var y = b.replace(/^the /i, ""); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/currency.js b/sorting/currency.js new file mode 100644 index 0000000..57c360d --- /dev/null +++ b/sorting/currency.js @@ -0,0 +1,31 @@ +/** + * This plug-in will provide numeric sorting for currency columns (either + * detected automatically with the currency type detection plug-in or set + * manually) while taking account of the currency symbol ($ or £ by default). + * @name Currency + * @author Allan Jardine + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "currency-asc": function ( a, b ) { + /* Remove any formatting */ + var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" ); + var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" ); + + /* Parse and return */ + x = parseFloat( x ); + y = parseFloat( y ); + + return x - y; + }, + + "currency-desc": function ( a, b ) { + var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" ); + var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" ); + + x = parseFloat( x ); + y = parseFloat( y ); + + return y - x; + } +} ); diff --git a/sorting/custom-data-source/dom-checkbox.js b/sorting/custom-data-source/dom-checkbox.js new file mode 100644 index 0000000..8b47afe --- /dev/null +++ b/sorting/custom-data-source/dom-checkbox.js @@ -0,0 +1,15 @@ +/** + * Read information from a column of checkboxes (input elements with type + * checkbox) and return an array to use as a basis for sorting. + * @name Checkbox data source + * @author Allan Jardine + */ + +$.fn.dataTableExt.afnSortData['dom-checkbox'] = function ( oSettings, iColumn ) +{ + var aData = []; + $( 'td:eq('+iColumn+') input', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () { + aData.push( this.checked==true ? "1" : "0" ); + } ); + return aData; +}; diff --git a/sorting/custom-data-source/dom-select.js b/sorting/custom-data-source/dom-select.js new file mode 100644 index 0000000..87048e6 --- /dev/null +++ b/sorting/custom-data-source/dom-select.js @@ -0,0 +1,15 @@ +/** + * Read information from a column of select (drop down) menus and return an + * array to use as a basis for sorting. + * @name Select menu data source + * @author Allan Jardine + */ + +$.fn.dataTableExt.afnSortData['dom-select'] = function ( oSettings, iColumn ) +{ + var aData = []; + $( 'td:eq('+iColumn+') select', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () { + aData.push( $(this).val() ); + } ); + return aData; +}; diff --git a/sorting/custom-data-source/dom-text.js b/sorting/custom-data-source/dom-text.js new file mode 100644 index 0000000..4e42d80 --- /dev/null +++ b/sorting/custom-data-source/dom-text.js @@ -0,0 +1,15 @@ +/** + * Read information from a column of input (type text) elements and return an + * array to use as a basis for sorting. + * @name Input element data source + * @author Allan Jardine + */ + +$.fn.dataTableExt.afnSortData['dom-text'] = function ( oSettings, iColumn ) +{ + var aData = []; + $( 'td:eq('+iColumn+') input', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () { + aData.push( this.value ); + } ); + return aData; +}; diff --git a/sorting/date-eu.js b/sorting/date-eu.js new file mode 100644 index 0000000..acbb332 --- /dev/null +++ b/sorting/date-eu.js @@ -0,0 +1,60 @@ +/** + * Similar to the Date (dd/mm/YY) data sorting plug-in, this plug-in offers + * additional flexibility with support for spaces between the values and + * either . or / notation for the separators. + * @name Date (dd . mm[ . YYYY]) + * @author Robert Sedovšek + */ + + (function(){ + +function calculate_date(date) { + var date = date.replace(" ", ""); + + if (date.indexOf('.') > 0) { + /*date a, format dd.mn.(yyyy) ; (year is optional)*/ + var eu_date = date.split('.'); + } else { + /*date a, format dd/mn/(yyyy) ; (year is optional)*/ + var eu_date = date.split('/'); + } + + /*year (optional)*/ + if (eu_date[2]) { + var year = eu_date[2]; + } else { + var year = 0; + } + + /*month*/ + var month = eu_date[1]; + if (month.length == 1) { + month = 0+month; + } + + /*day*/ + var day = eu_date[0]; + if (day.length == 1) { + day = 0+day; + } + + return (year + month + day) * 1; +} + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "date-eu-asc": function ( a, b ) { + x = calculate_date(a); + y = calculate_date(b); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "date-eu-desc": function ( a, b ) { + x = calculate_date(a); + y = calculate_date(b); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); + +}()); diff --git a/sorting/date-euro.js b/sorting/date-euro.js new file mode 100644 index 0000000..2090ab6 --- /dev/null +++ b/sorting/date-euro.js @@ -0,0 +1,54 @@ +/** + * This plug-in will provide date sorting for the "dd/mm/YYY hh:ii:ss" + * formatting, which is common in France and other European countries. It can + * also be quickly adapted for other formatting as required. Furthermore, this + * date sorting plug-in allows for empty values in the column. + * @name Date (dd/mm/YYY hh:ii:ss) + * @author Ronan Guilloux + */ + + jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "date-euro-asc": function ( a, b ) { + if ($.trim(a) != '') { + var frDatea = $.trim(a).split(' '); + var frTimea = frDatea[1].split(':'); + var frDatea2 = frDatea[0].split('/'); + var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1; + } else { + var x = 10000000000000; // = l'an 1000 ... + } + + if ($.trim(b) != '') { + var frDateb = $.trim(b).split(' '); + var frTimeb = frDateb[1].split(':'); + frDateb = frDateb[0].split('/'); + var y = (frDateb[2] + frDateb[1] + frDateb[0] + frTimeb[0] + frTimeb[1] + frTimeb[2]) * 1; + } else { + var y = 10000000000000; + } + var z = ((x < y) ? -1 : ((x > y) ? 1 : 0)); + return z; + }, + + "date-euro-desc": function ( a, b ) { + if ($.trim(a) != '') { + var frDatea = $.trim(a).split(' '); + var frTimea = frDatea[1].split(':'); + var frDatea2 = frDatea[0].split('/'); + var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1; + } else { + var x = 10000000000000; + } + + if ($.trim(b) != '') { + var frDateb = $.trim(b).split(' '); + var frTimeb = frDateb[1].split(':'); + frDateb = frDateb[0].split('/'); + var y = (frDateb[2] + frDateb[1] + frDateb[0] + frTimeb[0] + frTimeb[1] + frTimeb[2]) * 1; + } else { + var y = 10000000000000; + } + var z = ((x < y) ? 1 : ((x > y) ? -1 : 0)); + return z; + } +} ); diff --git a/sorting/date-uk.js b/sorting/date-uk.js new file mode 100644 index 0000000..26b3027 --- /dev/null +++ b/sorting/date-uk.js @@ -0,0 +1,32 @@ +/** + * DataTables internal date sorting replies on Date.parse() which is part of + * the Javascript language, but you may wish to sort on dates which is doesn't + * recognise. The following is a plug-in for sorting dates in the format + * dd/mm/yy. + * + * An automatic type detection plug-in is available for this sorting plug-in. + * @name Date (dd/mm/YY) + * @author Andy McMaster + */ + + jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "date-uk-asc": function ( a, b ) { + var ukDatea = a.split('/'); + var ukDateb = b.split('/'); + + var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1; + var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1; + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "date-uk-desc": function ( a, b ) { + var ukDatea = a.split('/'); + var ukDateb = b.split('/'); + + var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1; + var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1; + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/enum.js b/sorting/enum.js new file mode 100644 index 0000000..06897ce --- /dev/null +++ b/sorting/enum.js @@ -0,0 +1,38 @@ +/** + * Sort by priority through an enumerated list. In this case the words 'High', + * 'Medium' and 'Low' are used and thus sorted in priority order. This works + * by converting the works to a numerical value and then sorting based on that + * value. + * @name enum + * @author Allan Jardine + */ + +(function() { + +function priority( a ) { + // Add / alter the switch statement below to match your enum list + switch( a ) { + case "High": return 1; + case "Medium": return 2; + case "Low": return 3; + default: return 4; + } +} + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "enum-asc": function ( a, b ) { + var x = priority( a ); + var y = priority( b ); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "enum-desc": function ( a, b ) { + var x = priority( a ); + var y = priority( b ); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); + +}()); \ No newline at end of file diff --git a/sorting/file-size.js b/sorting/file-size.js new file mode 100644 index 0000000..c4b6d8d --- /dev/null +++ b/sorting/file-size.js @@ -0,0 +1,41 @@ +/** + * When dealing with computer file sizes, it is common to append a post fix + * such as KB, MB or GB to a string in order to easily denote the order of + * magnitude of the file size. This plug-in allows sorting to take these + * indicates of size into account. A counterpart type detection plug-in + * is also available. + * @name File size + * @author anjibman + */ + + jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "file-size-asc": function ( a, b ) { + var x = a.substring(0,a.length - 2); + var y = b.substring(0,b.length - 2); + + var x_unit = (a.substring(a.length - 2, a.length) == "MB" ? + 1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1)); + var y_unit = (b.substring(b.length - 2, b.length) == "MB" ? + 1000 : (b.substring(b.length - 2, b.length) == "GB" ? 1000000 : 1)); + + x = parseInt( x * x_unit ); + y = parseInt( y * y_unit ); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "file-size-desc": function ( a, b ) { + var x = a.substring(0,a.length - 2); + var y = b.substring(0,b.length - 2); + + var x_unit = (a.substring(a.length - 2, a.length) == "MB" ? + 1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1)); + var y_unit = (b.substring(b.length - 2, b.length) == "MB" ? + 1000 : (b.substring(b.length - 2, b.length) == "GB" ? 1000000 : 1)); + + x = parseInt( x * x_unit); + y = parseInt( y * y_unit); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/ip-address.js b/sorting/ip-address.js new file mode 100644 index 0000000..283068c --- /dev/null +++ b/sorting/ip-address.js @@ -0,0 +1,62 @@ +/** + * Sorts a column containing IP addresses in typical dot notation. This can + * be most useful when using DataTables for a networking application, and + * reporting information containing IP address. Also has a matching type + * detection plug-in for automatic type detection. + * @name IP addresses + * @author Brad Wasson + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "ip-address-asc": function ( a, b ) { + var m = a.split("."), x = ""; + var n = b.split("."), y = ""; + for(var i = 0; i < m.length; i++) { + var item = m[i]; + if(item.length == 1) { + x += "00" + item; + } else if(item.length == 2) { + x += "0" + item; + } else { + x += item; + } + } + for(var i = 0; i < n.length; i++) { + var item = n[i]; + if(item.length == 1) { + y += "00" + item; + } else if(item.length == 2) { + y += "0" + item; + } else { + y += item; + } + } + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "ip-address-desc": function ( a, b ) { + var m = a.split("."), x = ""; + var n = b.split("."), y = ""; + for(var i = 0; i < m.length; i++) { + var item = m[i]; + if(item.length == 1) { + x += "00" + item; + } else if (item.length == 2) { + x += "0" + item; + } else { + x += item; + } + } + for(var i = 0; i < n.length; i++) { + var item = n[i]; + if(item.length == 1) { + y += "00" + item; + } else if (item.length == 2) { + y += "0" + item; + } else { + y += item; + } + } + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/monthYear.js b/sorting/monthYear.js new file mode 100644 index 0000000..3b41413 --- /dev/null +++ b/sorting/monthYear.js @@ -0,0 +1,22 @@ +/** + * Often a list of data which has titles in it (books, albums etc) will have + * the word "the" at the start of some individual titles, which you don't want + * to include in your sorting order. This plug-in will strip the word "the" + * from the start of a string and sort on what is left. + * @name Month / year sorting + * @author Michael Motek + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "monthYear-asc": function ( a, b ) { + a = new Date('01 '+a); + b = new Date('01 '+b); + return ((a < b) ? -1 : ((a > b) ? 1 : 0)); + }, + + "monthYear-desc": function ( a, b ) { + a = new Date('01 '+a); + b = new Date('01 '+b); + return ((a < b) ? 1 : ((a > b) ? -1 : 0)); + } +} ); diff --git a/sorting/natural.js b/sorting/natural.js new file mode 100644 index 0000000..ef2042f --- /dev/null +++ b/sorting/natural.js @@ -0,0 +1,65 @@ +/** + * Often a list of data which has titles in it (books, albums etc) will have + * the word "the" at the start of some individual titles, which you don't want + * to include in your sorting order. This plug-in will strip the word "the" + * from the start of a string and sort on what is left. + * @name Natural sorting + * @author Jim Palmer + */ + +(function() { + +/* + * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license + * Author: Jim Palmer (based on chunking idea from Dave Koelle) + * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo + * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js + */ +function naturalSort (a, b) { + var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi, + sre = /(^[ ]*|[ ]*$)/g, + dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, + hre = /^0x[0-9a-f]+$/i, + ore = /^0/, + // convert all to strings and trim() + x = a.toString().replace(sre, '') || '', + y = b.toString().replace(sre, '') || '', + // chunk/tokenize + xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + // numeric, hex or date detection + xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)), + yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null; + // first try and sort Hex codes or Dates + if (yD) + if ( xD < yD ) return -1; + else if ( xD > yD ) return 1; + // natural sorting through split numeric strings and default strings + for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { + // find floats not starting with '0', string or 0 if not defined (Clint Priest) + oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0; + oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0; + // handle numeric vs string comparison - number < string - (Kyle Adams) + if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1; + // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' + else if (typeof oFxNcL !== typeof oFyNcL) { + oFxNcL += ''; + oFyNcL += ''; + } + if (oFxNcL < oFyNcL) return -1; + if (oFxNcL > oFyNcL) return 1; + } + return 0; +} + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "natural-asc": function ( a, b ) { + return naturalSort(a,b); + }, + + "natural-desc": function ( a, b ) { + return naturalSort(a,b) * -1; + } +} ); + +}()); diff --git a/sorting/num-html.js b/sorting/num-html.js new file mode 100644 index 0000000..b779c13 --- /dev/null +++ b/sorting/num-html.js @@ -0,0 +1,29 @@ +/** + * This sorting plug-in allows for HTML tags with numeric data. With the 'html' + * type it will strip the HTML and then sorts by strings, with this type it + * strips the HTML and then sorts by numbers. Note also that this sorting + * plug-in has an equivalent type detection plug-in which can make integration + * easier. + * @name Numbers with HTML + * @author Allan Jardine + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "num-html-asc": function ( a, b ) { + var x = a.replace( /<.*?>/g, "" ); + var y = b.replace( /<.*?>/g, "" ); + x = parseFloat( x ); + y = parseFloat( y ); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "num-html-desc": function ( a, b ) { + var x = a.replace( /<.*?>/g, "" ); + var y = b.replace( /<.*?>/g, "" ); + x = parseFloat( x ); + y = parseFloat( y ); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/numeric-comma.js b/sorting/numeric-comma.js new file mode 100644 index 0000000..aca68c0 --- /dev/null +++ b/sorting/numeric-comma.js @@ -0,0 +1,28 @@ +/** + * It is not uncommon for non-English speaking countries to use a comma for a + * decimal place. This sorting plug-in shows how that can be taken account of in + * sorting by adding the type 'numeric-comma' to DataTables. A type detection + * plug-in for this sorting method is provided below. + * @name Commas for decimal place + * @author Allan Jardine + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "numeric-comma-asc": function ( a, b ) { + var x = (a == "-") ? 0 : a.replace( /,/, "." ); + var y = (b == "-") ? 0 : b.replace( /,/, "." ); + x = parseFloat( x ); + y = parseFloat( y ); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "numeric-comma-desc": function ( a, b ) { + var x = (a == "-") ? 0 : a.replace( /,/, "." ); + var y = (b == "-") ? 0 : b.replace( /,/, "." ); + x = parseFloat( x ); + y = parseFloat( y ); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/percent.js b/sorting/percent.js new file mode 100644 index 0000000..36afc35 --- /dev/null +++ b/sorting/percent.js @@ -0,0 +1,25 @@ +/** + * Sort numeric data which has a percent sign with it. + * @name Percentage + * @author Jonathan Romley + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "percent-asc": function ( a, b ) { + var x = (a == "-") ? 0 : a.replace( /%/, "" ); + var y = (b == "-") ? 0 : b.replace( /%/, "" ); + x = parseFloat( x ); + y = parseFloat( y ); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "percent-desc": function ( a, b ) { + var x = (a == "-") ? 0 : a.replace( /%/, "" ); + var y = (b == "-") ? 0 : b.replace( /%/, "" ); + x = parseFloat( x ); + y = parseFloat( y ); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/persian.js b/sorting/persian.js new file mode 100644 index 0000000..18c65df --- /dev/null +++ b/sorting/persian.js @@ -0,0 +1,50 @@ +/** + * Sorting in Javascript can be difficult to get right with non-Roman + * characters - for which special consideration must be made. This plug-in + * performs correct sorting on Persian characters. + * @name Persian + * @author Afshin Mehrabani + */ + +(function(){ + +var persianSort = [ 'آ', 'ا', 'ب', 'پ', 'ت', 'ث', 'ج', 'چ', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'ژ', + 'س', 'ش', 'ص', 'ط', 'ظ', 'ع', 'غ', 'ف', 'ق', 'ک', 'گ', 'ل', 'م', 'ن', 'و', 'ه', 'ی', 'ي' ]; + +function GetUniCode(source) { + source = $.trim(source); + result = ''; + for (i = 0; i < source.length; i++) { + //Check and fix IE indexOf bug + if (!Array.indexOf) { + var index = jQuery.inArray(source.charAt(i), persianSort); + }else{ + var index = persianSort.indexOf(source.charAt(i)); + } + if (index < 0) { + index = source.charCodeAt(i); + } + if (index < 10) + index = '0' + index; + result += '00' + index; + } + return 'a' + result; +}; + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "pstring-asc": function ( a, b ) { + var x = GetUniCode(a.toLowerCase()); + var y = GetUniCode(b.toLowerCase()); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "pstring-desc": function ( a, b ) { + var x = GetUniCode(a.toLowerCase()); + var y = GetUniCode(b.toLowerCase()); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); + +}()); \ No newline at end of file diff --git a/sorting/scientific.js b/sorting/scientific.js new file mode 100644 index 0000000..bfa2130 --- /dev/null +++ b/sorting/scientific.js @@ -0,0 +1,22 @@ +/** + * This plug-in will treat numbers which are in scientific notation (for + * example 1E-10, 1.2E6 etc) and sort them numerically. + * @name Scientific notation sorting + * @author Nick Schurch + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "scientific-asc": function ( a, b ) { + var x = parseFloat(a); + var y = parseFloat(b); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "scientific-desc": function ( a, b ) { + var x = parseFloat(a); + var y = parseFloat(b); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/signed-num.js b/sorting/signed-num.js new file mode 100644 index 0000000..d35ae82 --- /dev/null +++ b/sorting/signed-num.js @@ -0,0 +1,21 @@ +/** + * Although DataTables' internal numeric sorting works no problem on negative + * numbers, it does not accept positively signed numbers. This plug-in will + * sort just such data numerically. + * @name Fully signed numbers sorting + * @author Allan Jardine + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "signed-num-asc": function ( a, b ) { + var x = (a=="-" || a==="") ? 0 : a.replace('+','')*1; + var y = (b=="-" || b==="") ? 0 : b.replace('+','')*1; + return x - y; + }, + + "signed-num-desc": function ( a, b ) { + var x = (a=="-" || a==="") ? 0 : a.replace('+','')*1; + var y = (b=="-" || b==="") ? 0 : b.replace('+','')*1; + return y - x; + } +} ); diff --git a/sorting/title-numeric.js b/sorting/title-numeric.js new file mode 100644 index 0000000..3f90838 --- /dev/null +++ b/sorting/title-numeric.js @@ -0,0 +1,31 @@ +/** + * An alternative to the formatted number sorting function above (particularly + * useful when considering localisation which uses dots/periods for 10^3 + * separation rather than decimal places). Another method of overcoming it + * difficulties of sorting formatted numbers is to have the data to be sorted + * upon separate from the visual data. This sorting function pair will use the + * 'title' attribute of en empty span element (or anything else) to sort + * numerically (for example 1'000'000). + * @name Hidden title numeric sorting + * @author Allan Jardine + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "title-numeric-asc": function ( a, b ) { + var x = a.match(/title="*(-?[0-9\.]+)/)[1]; + var y = b.match(/title="*(-?[0-9\.]+)/)[1]; + x = parseFloat( x ); + y = parseFloat( y ); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "title-numeric-desc": function ( a, b ) { + var x = a.match(/title="*(-?[0-9\.]+)/)[1]; + var y = b.match(/title="*(-?[0-9\.]+)/)[1]; + x = parseFloat( x ); + y = parseFloat( y ); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} ); diff --git a/sorting/title-string.js b/sorting/title-string.js new file mode 100644 index 0000000..01c231c --- /dev/null +++ b/sorting/title-string.js @@ -0,0 +1,24 @@ +/** + * Just like the "hidden title numeric sorting" plug-in, this sorting plug-in + * will take the information to be sorted on from the title attribute of a span + * element. The only difference is that it is string based sorting rather than + * numeric. + * @name Hidden title string sorting + * @author Allan Jardine + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "title-string-asc": function ( a, b ) { + var x = a.match(/title="(.*?)"/)[1].toLowerCase(); + var y = b.match(/title="(.*?)"/)[1].toLowerCase(); + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }, + + "title-string-desc": function ( a, b ) { + var x = a.match(/title="(.*?)"/)[1].toLowerCase(); + var y = b.match(/title="(.*?)"/)[1].toLowerCase(); + + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + } +} );