reverse()
JavaScript code:
var hatArray = ["hard", "cap", "top", "fedora"];
for (var i = 0; i < hatArray.length; i++)
{
document.write(hatArray[i], " ");
}
hatArray.reverse(); // reverses the array itself
for (var i = 0; i < hatArray.length; i++)
{
document.write(hatArray[i], " ");
}
Results:
hard cap top fedora
fedora top cap hard sort()
String sort example:
var list = new Array("bat", "caw", "ca", "cat", "c", "zebra", 6, 50, "Bat", "Zebra");
list.sort();
document.write(list.toString());
Result:
50,6,Bat,Zebra,bat,c,ca,cat,caw,zebra
Numeric sort example:
var numbers = new Array(7, 70, 4, 40, 100);
numbers.sort(compare_two_numbers);
document.write(numbers.toString());
function compare_two_numbers(first_parameter, second_parameter)
{
var return_value;
var first_number = parseFloat(first_parameter); // make sure it is numeric
var second_number = parseFloat(second_parameter); // make sure it is numeric
if ( first_number < second_number )
{ return_value = -1; }
else if ( first_number == second_number )
{ return_value = 0; }
else
{ return_value = 1; }
return return_value;
}
Result:
4,7,40,70,100