Can someone tell me why this JavaScript code isn't lining up an array in order?

Posted by DarkLightA on Stack Overflow See other posts from Stack Overflow or by DarkLightA
Published on 2010-12-29T09:18:04Z Indexed on 2010/12/29 9:54 UTC
Read the original article Hit count: 225

Filed under:
|

Live code: http://jsfiddle.net/fCUZC/

//INPUT ARRAY:
var input = [28,32,21,11,8,2,14,32,64];
//VARIABLE DECLARATION. a = highest number so far, b = position of that number
entireLoop:
for (var i = 1; i<=input.length; i++)
{
    if(input[i] > input[i-1])
    {
        for(var o = i; o>=0; o--)
        {
            if(input[i-1] > input[o])
            {
                input.splice(i,0,input[o]);
                input.splice((o+1),1);
                continue entireLoop;
            }
            else if(input[o] > input[0])
            {
                input.splice(0,0,input[o]);
                input.splice((o+1),1);
                continue entireLoop;
            }

        }
    }
}
document.write(input);

I'm trying to order the array from largest to smallest, but there's a 32 stuck somewhere. I know there's the sort method, but I'm a newbie and want to try this for myself.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about array-sorting