0

I am using Jim Palmer's natural sort (http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm/) and I have an array ['1a','1b','1c',...] etc. The problem I am having is that it sorts 1d, 1e, 1f incorrectly. I have found that every alpha numeric comparison returns either 1 or -1 except for 1d,1e,1f which always returns 0, does anyone know why this could be occuring?

Edit: http://jsbin.com/peviteyifa/edit?html,js,console,output here is an example this is a pretty specific problem I am having related to jquery datatables

when I try to sort columnDefs: [{targets: 0, type: 'natural'}]

Char
  • 3
  • 2
  • 3
    Can you post a code sample demonstrating this? – Halcyon Mar 24 '17 at 16:46
  • Yes one moment @Halcyon – Char Mar 24 '17 at 16:49
  • 1
    That code looks a little scary to me. I doesn't just do naturalsort, it also tries to sort by date and who knows what else. This line: `/(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,` seems to contain some kind of exception for `df` and `e`. If found this: https://github.com/Bill4Time/javascript-natural-sort/blob/master/naturalSort.js it looks a like the version you posted but it's newer and may not contain the bug. – Halcyon Mar 24 '17 at 16:50
  • `0x1d` as opposed to `1d` in decimal form? . 1d(dec) 1f(float) are the same (1e[0]) too... – corn3lius Mar 24 '17 at 16:51
  • Do you need all of that extra logic? http://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array (order with that one is swapped from what you have) – epascarello Mar 24 '17 at 16:51
  • @Halcyon I am going off of the example OP gave which is two simple numbers/letters. Not saying it is a perfect fit, but other one might be overkill (and slow) – epascarello Mar 24 '17 at 16:54
  • @Halcyon extremely sorry my computer crashed twice while setting this up http://jsbin.com/peviteyifa/edit?html,js,console,output – Char Mar 24 '17 at 17:30
  • @Halcyon just noticed your comment you are correct, if I remove the regex for df and e it works. What I am using is way over kill for what I need youre correct, if I could give you the answer for this I would thank you for solving my issue I cant tell you how long ive been over looking this. – Char Mar 24 '17 at 17:36

2 Answers2

0

There must be some issue with your code itself. please share the code for better investigation.

Also If you want to check out the working natural sort algorithm implementation.

excerpt:

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 
    oFxNcL = isNaN(xN[cLoc]) ? xN[cLoc] || 0 : parseFloat(xN[cLoc]) || 0;
    oFyNcL = isNaN(yN[cLoc]) ? yN[cLoc] || 0 : parseFloat(yN[cLoc]) || 0;
    // handle numeric vs string comparison - number < string 
    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;
}

For further understanding go to this JSFiddle

SBhalla
  • 31
  • 4
0

This regular expression seems to have a special case for d, e and f:

re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi
                            ^^  ^
Halcyon
  • 57,230
  • 10
  • 89
  • 128