├── LICENSE ├── README.md ├── index.html ├── naturalSort.js ├── speed-tests.html └── unit-tests.html /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2008-2016 Jim Palmer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [Demo - Run QUnit test suite](http://overset.github.io/javascript-natural-sort/unit-tests.html) 3 | 4 | ### Simple numerics 5 | 6 | ```javascript 7 | >>> ['10',9,2,'1','4'].sort(naturalSort) 8 | ['1',2,'4',9,'10'] 9 | ``` 10 | 11 | ### Floats 12 | 13 | ```javascript 14 | >>> ['10.0401',10.022,10.042,'10.021999'].sort(naturalSort) 15 | ['10.021999',10.022,'10.0401',10.042] 16 | ``` 17 | 18 | ### Float & decimal notation 19 | 20 | ```javascript 21 | >>> ['10.04f','10.039F','10.038d','10.037D'].sort(naturalSort) 22 | ['10.037D','10.038d','10.039F','10.04f'] 23 | ``` 24 | 25 | ### Scientific notation 26 | 27 | ```javascript 28 | >>> ['1.528535047e5','1.528535047e7','1.528535047e3'].sort(naturalSort) 29 | ['1.528535047e3','1.528535047e5','1.528535047e7'] 30 | ``` 31 | 32 | ### IP addresses 33 | 34 | ```javascript 35 | >>> ['192.168.0.100','192.168.0.1','192.168.1.1'].sort(naturalSort) 36 | ['192.168.0.1','192.168.0.100','192.168.1.1'] 37 | ``` 38 | 39 | ### Filenames 40 | 41 | ```javascript 42 | >>> ['car.mov','01alpha.sgi','001alpha.sgi','my.string_41299.tif'].sort(naturalSort) 43 | ['001alpha.sgi','01alpha.sgi','car.mov','my.string_41299.tif' 44 | ``` 45 | 46 | ### Dates 47 | 48 | ```javascript 49 | >>> ['10/12/2008','10/11/2008','10/11/2007','10/12/2007'].sort(naturalSort) 50 | ['10/11/2007', '10/12/2007', '10/11/2008', '10/12/2008'] 51 | ``` 52 | 53 | ### Money 54 | 55 | ```javascript 56 | >>> ['$10002.00','$10001.02','$10001.01'].sort(naturalSort) 57 | ['$10001.01','$10001.02','$10002.00'] 58 | ``` 59 | 60 | ### Movie Titles 61 | 62 | ```javascript 63 | >>> ['1 Title - The Big Lebowski','1 Title - Gattaca','1 Title - Last Picture Show'].sort(naturalSort) 64 | ['1 Title - Gattaca','1 Title - Last Picture Show','1 Title - The Big Lebowski'] 65 | ``` 66 | 67 | ### By default - case-sensitive sorting 68 | 69 | ```javascript 70 | >>> ['a', 'B'].sort(naturalSort); 71 | ['B', 'a'] 72 | ``` 73 | 74 | ### To enable case-insensitive sorting 75 | ```javascript 76 | >>> naturalSort.insensitive = true; 77 | >>> ['a', 'B'].sort(naturalSort); 78 | ['a', 'B'] 79 | ``` 80 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | overset/javascript-natural-sort @ GitHub 7 | 8 | 31 | 32 | 33 | 34 | Fork me on GitHub 35 | 36 |
37 | 38 |
39 | 40 | 41 | 42 | 43 |
44 | 45 |

javascript-natural-sort 46 | by overset

47 | 48 |
49 | A fast and lightweight function to be used in conjunction with the Array.sort() using the browser's default bubble sort. This will properly sort padded numbers, numbers preceding text, dates, floats, etc. 50 |
51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |

License

60 |

MIT

61 | 62 | 63 | 64 |

Authors

65 |

Jim Palmer (jimpalmer@gmail.com)

66 | 67 | 68 | 69 |

Contact

70 |

Jim Palmer (jimpalmer@gmail.com)

71 | 72 | 73 |

Download

74 |

75 | You can download this project in either 76 | zip or 77 | tar formats. 78 |

79 |

You can also clone the project with Git 80 | by running: 81 |

$ git clone git://github.com/overset/javascript-natural-sort
82 |

83 | 84 | 87 | 88 |
89 | 90 | 94 | " 99 | 100 | 101 | -------------------------------------------------------------------------------- /naturalSort.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Natural Sort algorithm for Javascript - Version 0.8.1 - Released under MIT license 3 | * Author: Jim Palmer (based on chunking idea from Dave Koelle) 4 | */ 5 | function naturalSort (a, b) { 6 | var re = /(^([+\-]?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?(?=\D|\s|$))|^0x[\da-fA-F]+$|\d+)/g, 7 | sre = /^\s+|\s+$/g, // trim pre-post whitespace 8 | snre = /\s+/g, // normalize all whitespace to single ' ' character 9 | dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, 10 | hre = /^0x[0-9a-f]+$/i, 11 | ore = /^0/, 12 | i = function(s) { 13 | return (naturalSort.insensitive && ('' + s).toLowerCase() || '' + s).replace(sre, ''); 14 | }, 15 | // convert all to strings strip whitespace 16 | x = i(a), 17 | y = i(b), 18 | // chunk/tokenize 19 | xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), 20 | yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), 21 | // numeric, hex or date detection 22 | xD = parseInt(x.match(hre), 16) || (xN.length !== 1 && Date.parse(x)), 23 | yD = parseInt(y.match(hre), 16) || xD && y.match(dre) && Date.parse(y) || null, 24 | normChunk = function(s, l) { 25 | // normalize spaces; find floats not starting with '0', string or 0 if not defined (Clint Priest) 26 | return (!s.match(ore) || l == 1) && parseFloat(s) || s.replace(snre, ' ').replace(sre, '') || 0; 27 | }, 28 | oFxNcL, oFyNcL; 29 | // first try and sort Hex codes or Dates 30 | if (yD) { 31 | if (xD < yD) { return -1; } 32 | else if (xD > yD) { return 1; } 33 | } 34 | // natural sorting through split numeric strings and default strings 35 | for(var cLoc = 0, xNl = xN.length, yNl = yN.length, numS = Math.max(xNl, yNl); cLoc < numS; cLoc++) { 36 | oFxNcL = normChunk(xN[cLoc] || '', xNl); 37 | oFyNcL = normChunk(yN[cLoc] || '', yNl); 38 | // handle numeric vs string comparison - number < string - (Kyle Adams) 39 | if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { 40 | return isNaN(oFxNcL) ? 1 : -1; 41 | } 42 | // if unicode use locale comparison 43 | if (/[^\x00-\x80]/.test(oFxNcL + oFyNcL) && oFxNcL.localeCompare) { 44 | var comp = oFxNcL.localeCompare(oFyNcL); 45 | return comp / Math.abs(comp); 46 | } 47 | if (oFxNcL < oFyNcL) { return -1; } 48 | else if (oFxNcL > oFyNcL) { return 1; } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /speed-tests.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | 12 | 13 | 179 | 180 | -------------------------------------------------------------------------------- /unit-tests.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | js-naturalsort test suite 5 | 6 | 7 | 8 | 9 | 476 | 477 | 478 |

js-naturalsort test suite

479 |

480 |

481 |
    482 | 483 | 484 | --------------------------------------------------------------------------------