JavaScript数组中的sort函数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<script>
		var arr=[0,1,2,5,10];
		function func(a,b){
			if(a>b){
				return 1;
			}
			else if(a<b){
				return -1;
			}
			else if(a==b){
				return 0;
			}
		}
		alert(arr.sort(func));//sort函数中引入比较函数
	</script>
	<body>
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/CWH0908/article/details/86770106