<html>
<head>
<title>Using the sort() method on numbers and strings</title>
<script type="text/javascript" language="javascript">
<!-- //
function defaultSort(elementX, elementY)
{
var element1 = elementX.toString();
var element2 = elementY.toString();
if (element1 < element2){
return -1;
}else if (element1 > element2) {
return 1;
}else{
return 0;
}
}
function SortElements() {
var mixture = new Array("red",4,"blue",2,"green",9);
mixture.sort(defaultSort);
var mj = mixture.join(", ");
alert(mj);
}
// -->
</script>
</head>
<body onload="SortElements()">
</body>
</html>
|