Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
/*
 * Natural Sort algorithm for Javascript
 *
 * Author: Lauri Rooden
 *
 * code source on https://github.com/litejs/natural-compare-lite
 *
 */
 var naturalSort = function(a, b) {
    if (a != b) for (var i, ca, cb = 1, ia = 0, ib = 0; cb;) {
        ca = a.charCodeAt(ia++) || 0;
        cb = b.charCodeAt(ib++) || 0;
        if (ca < 58 && ca > 47 && cb < 58 && cb > 47) {
            for (i = ia; ca = a.charCodeAt(ia), ca < 58 && ca > 47; ia++);
            ca = (a.slice(i - 1, ia) | 0) + 1;
            for (i = ib; cb = b.charCodeAt(ib), cb < 58 && cb > 47; ib++);
            cb = (b.slice(i - 1, ib) | 0) + 1;
        }
        if (ca != cb) return (ca < cb) ? -1 : 1;
    }
    return 0;
};
/*
 * Sample usages, using javascript's native array's .sort() method
 *
 * see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
 */
var stringArray = [' ciao', 'ciao', 'hello', 'hellothere', 'hello there'];
/* non-ASCII characters, i.e. strings with accented characters (e, é, è, a, ä, etc.),  */
var stringArrayWithNonAsciiChars = ['réservé', 'réserve', 'premier', 'cliché', 'communiqué', 'café', 'adieu'];
var numericStringArray = ['80', '9', '700'];
var stringAlphanumericalOne = [' ','9','1','ä','z','-','a','é',';'];
var stringAlphanumericalTwo = ['img ','img9','imga','img-' , 'imgz', 'imgä','img1', 'imgé','img;'];
var stringAlphanumericalThree = ['img 9','img19','imga9','img-9', 'imgz9', 'imgä9', 'imgé9','img;9','img99'];
console.log( stringArray.sort(naturalSort) );
console.log( stringArrayWithNonAsciiChars.sort(naturalSort));
console.log( numericStringArray.sort(naturalSort) );
console.log( stringAlphanumericalOne.sort(naturalSort) );
console.log( stringAlphanumericalTwo.sort(naturalSort) );
console.log( stringAlphanumericalThree.sort(naturalSort) );
/* Basic Latin characters http://en.wikipedia.org/wiki/List_of_Unicode_characters#Basic_Latin */
var basicLatin = [' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}'];
console.log( basicLatin.sort(naturalSort) );
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers