Case-Insensitive Comparison for the Array.Sort Method : Array : Language Basics JAVASCRIPT DHTML TUTORIALS


JAVASCRIPT DHTML TUTORIALS » Language Basics » Array »

 

Case-Insensitive Comparison for the Array.Sort Method



<HTML>
<HEAD>
<TITLE> 
Case-insensitive comparison 
</TITLE>
<HEAD>
<BODY>
<H1>
<SCRIPT
   var theArray = new Array("a","N","Mo","T""Mr. Smith""N""n""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 
      if (a < b
         return -
      return 0
    })
    document.write ("Custom sorted array: " + theArray)
   </SCRIPT>
</H1>
</BODY>
</HTML>




           
       



-

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

JAVASCRIPT DHTML TUTORIALS

 Navioo Language Basics
» Array