Last active
August 29, 2015 14:06
-
-
Save hongymagic/35c6093a7b0b48fb4c4b to your computer and use it in GitHub Desktop.
found this in ~/, saving it before removing. no idea what it does
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Compare two characters | |
// | |
// returns 0 if equal, -1 if x is before y, 1 otherwise. | |
function compareChars(x, y) { | |
if (x === y) { | |
return 0; | |
} | |
if (x === undefined) { | |
return 1; | |
} | |
if (y === undefined) { | |
return -1; | |
} | |
var xc = x.charCodeAt(0); | |
var yc = y.charCodeAt(0); | |
return xc < yc ? -1 : 1; | |
} | |
function compareStrings(x, y) { | |
x = x.split(''); | |
y = y.split(''); | |
var i = 0; | |
var ch_x = x[i]; | |
var ch_y = y[i]; | |
var result; | |
while (((result = compareChars(ch_x, ch_y)) === 0)) { | |
i += 1; | |
ch_x = x[i]; | |
ch_y = y[i]; | |
if (ch_x === undefined) { | |
if (ch_y === undefined) { | |
result = 0; | |
break; | |
} | |
result = -1; | |
break; | |
} | |
if (ch_y === undefined) { | |
result = 1; | |
break; | |
} | |
} | |
console.log('Found in ' + i + ' steps', result); | |
return result; | |
} | |
compareStrings('~/Scripts/src/components/accounts.js', '~/Scripts/src/components/accounts-services.js'); | |
compareStrings('~/Scripts/src/components/accounts.js', '~/Scripts/src/components/accounts.js'); |
It could just be implemented
function compareStrings(x, y) { if (x > y) return 1; if (y > x) return -1; return 0 }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It does a lexicographical comparison of two strings, returning 1 if the left is bigger, -1 if the right is bigger and 0 if they are equal.