If tomorrow never comes

The meaning of life is creation,which is independent an boundless.

导航

javascript数组排序

Posted on 2009-09-02 14:41  Brucegao  阅读(1095)  评论(0编辑  收藏  举报
      记得在半年前还在中软实习的时候,有一次考试我清晰的记得就是用javascript实现对数组的排序算法,那个时候自己不会用javascript,对javascript也只是知道一点点而已,今天虽然还是不是非常的精通javascript,但是至少那个时候的那个程序现在可以写出来,姑且算是一种进步吧,算法实现如下:

<html>
    
<head>

    
</head>

<body>
<script language="javascript">

    
var array = new Array(12,3,465,7,32,88,0,76,5,99,22);
    document.write(
"Before you Sorted:" + array + "<br>");

    
for(index in array)
    {
        
for(n in array)
        {
            
if(array[index]<array[n])
            {
                
var nTemp = array[index];
                array[index] 
= array[n];
                array[n] 
= nTemp;
            }
        }
    }
    document.write(
"After you Sorted:" + array);

</script>
</body>
</html>
本程序的输出结果为:
Before you Sorted:12,3,465,7,32,88,0,76,5,99,22
After you Sorted:  0,3,5,7,12,22,32,76,88,99,465