JavaScript Tutorial/Array/Sort
Содержание
Array.sort()
Syntax
array.sort()
array.sort(function)
Array.sort is case sensitive
<html>
<head>
<title>Using the sort() method</title>
<script type="text/javascript" language="javascript">
<!-- //
function SortElements() {
var colors = new Array("red","Red","blue","Blue","green","Green");
colors.sort();
var cj = colors.join(", ");
alert("The sorted array contains: " + cj);
}
// -->
</script>
</head>
<body onload="SortElements()">
</body>
</html>
Array.sort() with custom sorter
<html>
<head>
<title>Using the sort() method on numbers and strings</title>
<script type="text/javascript" language="javascript">
<!-- //
function defaultSort(elementX, elementY)
{
if (elementX < elementY)
return -1;
if (elementX > elementY)
return 1;
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>
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>
Sort a string array
<html>
<head>
<title>Using the sort() method</title>
<script type="text/javascript" language="javascript">
<!-- //
function SortElements(){
var colors = new Array("red","blue","green");
colors.sort();
var cj = colors.join(", ");
alert("The sorted array contains: " + cj);
}
// -->
</script>
</head>
<body onload="SortElements()">
</body>
</html>
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>
Using the sort() method on numbers and strings
<html>
<head>
<title>Using the sort() method on numbers and strings</title>
<script type="text/javascript" language="javascript">
<!-- //
function SortElements() {
var myNums = new Array(9,4,"B",11,301);
myNums.sort();
var mj = myNums.join(", ");
alert("The sorted array contains: " + mj);
}
// -->
</script>
</head>
<body onload="SortElements()">
</body>
</html>
Using the sort() method on numbers and strings with custom sorter
<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>