Created
June 15, 2011 16:22
-
-
Save jancassio/1027464 to your computer and use it in GitHub Desktop.
JavaScript String compare
This file contains 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 a string (a la c) with other and return -1 if smaller, 0 if equals or 1 if higher | |
* @author: Jan Cássio | [email protected] | |
* @usage | |
* "cupcake".compare("chocolate"); // returns 1; | |
* "sounds good".compare("sounds good"); // return 0; | |
* "10.1".compare("10.105"); // returns -1; | |
*/ | |
String.prototype.compare = function(other) | |
{ | |
if( typeof other !== "string" ) return null; | |
return (other > this ) ? -1 : Number( other < this ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment