js 数组按照指定的顺序,重新排序

工作中,碰到有数组,要重新按照指定顺序排序。网上找了很久没找到。自己就改了一个demo,也不知道以后还有没有用。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>

<script>
		var i;
		var mycars = new Array();
		mycars[0] = "123";
		mycars[1] = "有人吗";
		mycars[2] ="456";
			
		var mycars2 = new Array();
		for (i=0;i<mycars.length;i++){
			if(mycars[i]=="456"){
				mycars2[0] =mycars[i];	  
			}
			if(mycars[i]=="有人吗"){
				mycars2[1] =mycars[i];	  
			}
			if(mycars[i]=="123"){
				mycars2[2] =mycars[i];	  
			}
		}
		for (i=0;i<mycars2.length;i++){
			 document.write(mycars2[i] + "
"
); } </script> </body> </html>

你可能感兴趣的:(js常用方法收集,javascript)