Mega Code Archive

 
Categories / JavaScript Tutorial / Array
 

Case-insensitive comparison

< HTML> <HEAD> <TITLE> Case-insensitive comparison </TITLE> <HEAD>    <BODY>    <H1>    <SCRIPT>     var theArray = new Array("a","N","M","T","r", "A", "q", "A",2);     document.write ("Original array: " + theArray);     document.write ("<br>");     theArray.sort();     document.write ("Default Sorted array: " + theArray);     document.write ("<br>");     theArray.sort(function(x,y){       var a = String(x).toUpperCase();       var b = String(y).toUpperCase();       if (a > b)          return 1       if (a < b)          return -1       return 0;     });     document.write ("Custom sorted array: " + theArray);    </SCRIPT>    </H1>    </BODY> </HTML>