A Pen by Chris Stephens on CodePen.
Created
August 8, 2015 17:45
-
-
Save cstephe/2d33726d0e7e127749ed to your computer and use it in GitHub Desktop.
string compare
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
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