# Natural sort

## JavaScript performance comparison

Revision 12 of this test case created by

## Preparation code

``````<script src="http://membres-liglab.imag.fr/donsez/cours/exemplescourstechnoweb/js_securehash/md5.js"></script>
<script>
function alphanum(a, b) {
function chunkify(t) {
var tz = [], x = 0, y = -1, n = 0, i, j;

while (i = (j = t.charAt(x++)).charCodeAt(0)) {
var m = (i == 46 || (i >=48 && i <= 57));
if (m !== n) {
tz[++y] = "";
n = m;
}
tz[y] += j;
}
return tz;
}

var aa = chunkify(a);
var bb = chunkify(b);

for (x = 0; aa[x] && bb[x]; x++) {
if (aa[x] !== bb[x]) {
var c = Number(aa[x]), d = Number(bb[x]);
if (c == aa[x] && d == bb[x]) {
return c - d;
} else return (aa[x] > bb[x]) ? 1 : -1;
}
}
return aa.length - bb.length;
}

Array.prototype.alphanumSort = function(caseInsensitive) {
for (var z = 0, t; t = this[z]; z++) {
this[z] = [];
var x = 0, y = -1, n = 0, i, j;

while (i = (j = t.charAt(x++)).charCodeAt(0)) {
var m = (i == 46 || (i >=48 && i <= 57));
if (m !== n) {
this[z][++y] = "";
n = m;
}
this[z][y] += j;
}
}

this.sort(function(a, b) {
for (var x = 0, aa, bb; (aa = a[x]) && (bb = b[x]); x++) {
if (caseInsensitive) {
aa = aa.toLowerCase();
bb = bb.toLowerCase();
}
if (aa !== bb) {
var c = Number(aa), d = Number(bb);
if (c == aa && d == bb) {
return c - d;
} else return (aa > bb) ? 1 : -1;
}
}
return a.length - b.length;
});

for (var z = 0; z < this.length; z++)
this[z] = this[z].join("");
}

function a1Sort(a, b){
var cnt= 0, tem;
a= String(a).toLowerCase();
b= String(b).toLowerCase();
if(a== b) return 0;
if(/\d/.test(a) ||  /\d/.test(b)){
var Rx=  /^\d+(\.\d+)?/;
while(a.charAt(cnt)=== b.charAt(cnt) &&
!Rx.test(a.substring(cnt))){
cnt++;
}
a= a.substring(cnt);
b= b.substring(cnt);
if(Rx.test(a) || Rx.test(b)){
if(!Rx.test(a)) return a? 1: -1;
if(!Rx.test(b)) return b? -1: 1;
tem= parseFloat(a)-parseFloat(b);
if(tem!= 0) return tem;
a= a.replace(Rx,'');
b= b.replace(Rx,'');
if(/\d/.test(a) ||  /\d/.test(b)){
return a1Sort(a, b);
}
}
}
if(a== b) return 0;
return a> b? 1: -1;
}

// https://github.com/litejs/natural-compare-lite/
String.naturalCompare=function(e,f,l){function k(b,c,a){a=l&&l.indexOf(b.charAt(c));return-1<a?a+76:(a=b.charCodeAt(c)||0,45>a||127<a)?a:46>a?65:48>a?a-1:58>a?a+18:65>a?a-11:91>a?a+11:97>a?a-37:123>a?a+5:a-63}l=String.alphabet;if((e+="")!=(f+=""))for(var d,c,b=1,g=0,h=0;b;){c=k(e,g++);b=k(f,h++);if(!d&&76>c&&76>b&&66<c&&66<b){for(d=g;c=k(e,g),76>c&&65<c;g++);c=+e.slice(d-1,g);for(d=h;b=k(f,h),76>b&&65<b;h++);b=+f.slice(d-1,h)}d=66==c||66==b;if(c!=b)return c<b?-1:1}return 0};

function strcomp(a, b) {

if (a != b) for (var i, ca, cb = 1, ia = 0, ib = 0; cb;) {
ca = a.charAt(ia++) || 0
cb = b.charAt(ib++) || 0

if (ca && cb && !isNaN(ca) && !isNaN(cb)) {
for (i = ia; ca = a.charAt(ia), ca && !isNaN(ca); ia++);
ca = (a.slice(i - 1, ia) | 0) + 1
for (i = ib; cb = b.charAt(ib), cb && !isNaN(cb); ib++);
cb = (b.slice(i - 1, ib) | 0) + 1
}

if (ca != cb) return (ca < cb) ? -1 : 1
}
return 0
}

var bbb = [];
for (var iii = 0; iii < 1000; ++iii)
{
bbb.push(calcMD5(iii.toString()));
}
</script>
<script src="https://raw.github.com/kvz/phpjs/master/functions/strings/strnatcasecmp.js"></script>
<script src="https://raw.github.com/kvz/phpjs/master/functions/array/natcasesort.js"></script>

<script>
Benchmark.prototype.setup = function() {
var aaa = bbb.slice(0);
};

Benchmark.prototype.teardown = function() {
console.log(aaa);
};

</script>
``````

## Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
alphanum
``aaa.sort(alphanum);``
pending…
alphanumSort
``aaa.alphanumSort();``
pending…
a1sort
``aaa.sort(a1Sort);``
pending…
natcasesort
``natcasesort(aaa);``
pending…
String.naturalCompare
``````// https://github.com/litejs/natural-compare-lite/
aaa.sort(String.naturalCompare);``````
pending…
strcomp
``aaa.sort(strcomp);``
pending…

## Revisions

You can edit these tests or add even more tests to this page by appending `/edit` to the URL.