js实现升序和降序

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			function sortUp(a,b){
				return a-b;
			}
			function sortDown(a,b){
				return b-a;
			}
			var arr=new Array(6);
			arr[0]=10;
			arr[1]=5;
			arr[2]=8;
			arr[3]=30;
			arr[4]=55;
			arr[5]=32;
			// 升序
			document.write(arr.sort(sortUp));
			// 5,8,10,30,32,55
			//降序
			document.write(arr.sort(sortDown));
			// 55,32,30,10,8,5
		</script>
	</body>
</html>

posted @ 2019-05-07 11:39  lxystar  阅读(5496)  评论(0编辑  收藏  举报