DataTables / Plugins

Plug-ins for DataTables
Other
1.04k stars 1.74k forks source link

Natural sorting plugin should handle percentages #298

Open krid opened 7 years ago

krid commented 7 years ago

Numbers with percent signs get treated as text. This works OK if they're all positive (or all negative) but breaks when there's a mix of positive & negative values. A two character change to the number regex fixes the issue:

@@ -70,7 +70,7 @@
  * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
  */
 function naturalSort (a, b, html) {
-   var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
+   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,
DataTables commented 7 years ago

Looks good to me - thanks for this! Are you happy for this to be included under the MIT license?

krid commented 7 years ago

Do with it what you will. MIT is dandy. Thanks.