Skip to content

Instantly share code, notes, and snippets.

@jancassio
Created June 15, 2011 16:22
Show Gist options
  • Save jancassio/1027464 to your computer and use it in GitHub Desktop.
Save jancassio/1027464 to your computer and use it in GitHub Desktop.
JavaScript String compare
/**
* 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