Using an alphabetical sort() method on strings : Sort : Array JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Array » Sort »

 

Using an alphabetical sort() method on strings

















<html>
<head>
<title>Using an alphabetical sort() method on strings</title>
<script type="text/javascript" language="javascript">
<!-- //

function alphabetical(a, b)
{
     var A = a.toLowerCase();
     var B = b.toLowerCase();
     if (A < B){
        return -1;
     }else if (A > B){
       return  1;
     }else{
       return 0;
     }
}

function SortElements() {
var colors = new Array("red","Red","blue","Blue","green","Green");

colors.sort(alphabetical);

var cj = colors.join(", ");
alert(cj);
}
// -->
</script>
</head>
<body onload="SortElements()">

</body>
</html>







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Array
» Sort