Skip to content

Instantly share code, notes, and snippets.

@cstephe
Created August 8, 2015 17:45
Show Gist options
  • Save cstephe/2d33726d0e7e127749ed to your computer and use it in GitHub Desktop.
Save cstephe/2d33726d0e7e127749ed to your computer and use it in GitHub Desktop.
string compare
var testData = ['yeti', 'a dude', 'faceball 5000', 'zoolander', 'surfing', '', '90', '100']
var lexiconalCompare = function(a, b) {
if (typeof a === 'string' && typeof b === 'string') {
a = a.toLowerCase();
b = b.toLowerCase();
var bigLength = a.length > b.length ? a.length : b.length;
for (var x = 0; x < bigLength; x++) {
var aCharValue = a[x] ? a.charCodeAt(x) : 0,
bCharValue = b[x] ? b.charCodeAt(x) : 0
var charDifferenece = aCharValue - bCharValue;
if (charDifferenece) {
return charDifferenece;
}
}
return 0;
}
};
console.log(testData.sort(lexiconalCompare))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment