Permalink
Browse files

See #425 - nitpicking natural sorting algorithm.

Thanks to @alexlamsl for spotting these.
  • Loading branch information...
1 parent d0320e8 commit 68f4bd7cae1f6ca0136df18493b435c34c4101b3 @jakubpawlowicz committed Jan 11, 2017
Showing with 14 additions and 2 deletions.
  1. +2 −2 lib/utils/natural-compare.js
  2. +12 −0 test/utils/natural-compare-test.js
@@ -15,11 +15,11 @@ function naturalCompare(value1, value2) {
key2 = keys2[i];
if (key1 != key2) {
- return key1 > key2 ? 1 : (key1 === key2 ? 0 : -1);
+ return key1 > key2 ? 1 : -1;
}
}
- return keys1.length > keys2.length ? 1 : -1;
+ return keys1.length > keys2.length ? 1 : (keys1.length == keys2.length ? 0 : -1);
}
function tryParseInt(value) {
@@ -43,4 +43,16 @@ vows.describe(naturalCompare)
}
}
})
+ .addBatch({
+ 'objects': {
+ 'topic': [['a', 1], ['a0', 2], ['a0', 3], ['a1', 5], ['a0', 4], ['a0', 5], ['a0', 1]],
+ 'are sorted': function (list) {
+ var sortedList = list.sort(function (o1, o2) {
+ return naturalCompare(o1[0], o2[0]);
+ });
+
+ assert.deepEqual(sortedList, [['a', 1], ['a0', 2], ['a0', 3], ['a0', 4], ['a0', 5], ['a0', 1], ['a1', 5]]);
+ }
+ }
+ })
.export(module);

0 comments on commit 68f4bd7

Please sign in to comment.