Skip to content

Instantly share code, notes, and snippets.

@hongymagic
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongymagic/35c6093a7b0b48fb4c4b to your computer and use it in GitHub Desktop.
Save hongymagic/35c6093a7b0b48fb4c4b to your computer and use it in GitHub Desktop.
found this in ~/, saving it before removing. no idea what it does
//
// 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');
@Veedrac
Copy link

Veedrac commented Sep 23, 2014

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.

@Veedrac
Copy link

Veedrac commented Sep 23, 2014

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