# Natural sort

## JavaScript performance comparison

Revision 11 of this test case created

## 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//*! litejs.com/MIT-LICENSE.txt */String.naturalCompare=function(e,f){if(e!=f)for(var g,b,a=1,c=0,d=0;a;){b=e.charCodeAt(c++)||0;a=f.charCodeAt(d++)||0;if(58>b&&47<b&&58>a&&47<a){for(g=c;b=e.charCodeAt(c),58>b&&47<b;c++);b=(e.slice(g-1,c)|0)+1;for(g=d;a=f.charCodeAt(d),58>a&&47<a;d++);a=(f.slice(g-1,d)|0)+1}if(b!=a)return b<a?-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 unknown unknown
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. Here’s a list of current revisions for this page: