Permalink
Browse files

Re-think natural sort

  • Loading branch information...
1 parent 02e7065 commit 0c60c94f910e81f3c08805029a35962729c44173 Devin Torres committed Apr 9, 2010
Showing with 12 additions and 23 deletions.
  1. +1 −1 server/fotofoo.js
  2. +1 −1 server/lib/lrucache.js
  3. +1 −1 server/lib/magick.js
  4. +9 −20 server/lib/natsort.js
View
@@ -5,7 +5,7 @@ var sys = require('sys'),
multipart = require('./lib/multipart'),
lrucache = require('./lib/lrucache'),
magick = require('./lib/magick'),
- naturalSort = require('./lib/natsort').naturalSort,
+ naturalSort = require('./lib/natsort'),
router = require('./deps/node-router/lib/node-router');
var ROOT = path.join(__dirname, 'ROOT'),
View
@@ -6,7 +6,7 @@ LRUCache.Cache = function(maxsize) {
this._expires = {};
this._size = 0;
this._maxsize = maxsize || 1024;
- if (this._maxsize < 2) throw "max size must be > 2";
+ if (this._maxsize < 2) throw Error("max size must be > 2");
};
LRUCache.Cache.prototype = {
View
@@ -3,7 +3,7 @@ var childProcess = require('child_process');
var Magick = {};
Magick.Image = function (path) {
- if (!path) throw "Must provide an image path";
+ if (!path) throw Error("Must provide an image path");
this._path = path;
this._WHITESPACE_RE = /(\s)/g;
};
View
@@ -1,25 +1,14 @@
-var NATURAL_SORT_RE = /(-?[0-9\.]+)/g;
+var NUMBER_GROUPS = /(-?[0-9]*\.?[0-9]+)/g;
-// http://code.google.com/p/js-naturalsort/
-exports.naturalSort = function (a, b) {
- var x = a.toString().toLowerCase() || '',
- y = b.toString().toLowerCase() || '',
- nC = String.fromCharCode(0),
- xN = x.replace(NATURAL_SORT_RE, nC + '$1' + nC).split(nC),
- yN = y.replace(NATURAL_SORT_RE, nC + '$1' + nC).split(nC),
- xD = (new Date(x)).getTime(),
- yD = xD ? (new Date(y)).getTime() : null;
+var naturalSort = module.exports = function (a, b) {
+ var aa = String(a).split(NUMBER_GROUPS),
+ bb = String(b).split(NUMBER_GROUPS);
- if (yD) {
- if (xD < yD) return -1;
- else if (xD > yD) return 1;
- }
-
- for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
- var oFxNcL = parseFloat(xN[cLoc]) || xN[cLoc] || 0,
- oFyNcL = parseFloat(yN[cLoc]) || yN[cLoc] || 0;
- if (oFxNcL < oFyNcL) return -1;
- else if (oFxNcL > oFyNcL) return 1;
+ for (var i = 0, min = Math.min(aa.length, bb.length); i < min; i++) {
+ var x = parseFloat(aa[i]) || aa[i].toLowerCase(),
+ y = parseFloat(bb[i]) || bb[i].toLowerCase();
+ if (x < y) return -1;
+ else if (x > y) return 1;
}
return 0;

0 comments on commit 0c60c94

Please sign in to comment.