JavaScript数组遍历排序

发布网友

我来回答

1个回答

热心网友

<html>

<head>

<title>二维数组排序,好长时间没写JavaScript了</title>

</head>

<body>

<script>

var
arrayNum=[[22,33,414,55],[626,177,8,99,1000],[11,101,2,901]];

function
loopValue(loopOnestart,loopTwostart)
//循环数组的值

{

var
loopNum=checkLoop(loopOnestart,loopTwostart),x,y,changeNum;

if(loopNum==1)

{
x=loopOnestart;
y=loopTwostart+1;
}

else
if(loopNum==2){
x=loopOnestart+1;
y=0;
}

else
{
return
null;
}

for(;x<arrayNum.length;x++)

{

for(;y<arrayNum[x].length;y++)

{
if(arrayNum[loopOnestart][loopTwostart]>arrayNum[x][y])

{
//alert(arrayNum[loopOnestart][loopTwostart]+">"+arrayNum[x][y]);

changeNum=arrayNum[loopOnestart][loopTwostart];

arrayNum[loopOnestart][loopTwostart]=arrayNum[x][y];

arrayNum[x][y]=changeNum;

}

}
y=0;

}

}

function
loopPoint(){

//循环的指针

for(n=0;n<arrayNum.length;n++)

{

for(m=0;m<arrayNum[n].length;m++)

{

loopValue(n,m);

}

}

alert(arrayNum);

}

function
checkLoop(loopOnestart,loopTwostart){

//只需要判断一次就可以了

if(loopTwostart+1<arrayNum[loopOnestart].length)

{

return
1;

}else
if(loopTwostart+1==arrayNum[loopOnestart].length)

{

if(loopOnestart+1<arrayNum.length)

return
2;
else
return
3;
}

else{
return
0;
}

}

loopPoint();

</script>

</body>

</html>

写的有点复杂,但是是正确的!

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com