Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3618260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:43:44+00:00 2026-05-18T22:43:44+00:00

Live code: http://jsfiddle.net/fCUZC/ //INPUT ARRAY: var input = [28,32,21,11,8,2,14,32,64]; //VARIABLE DECLARATION. a = highest

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:43:45+00:00Added an answer on May 18, 2026 at 10:43 pm

    ** edit **
    First have a look at the Array’s native .sort() method. It leaves the original array intact and accepts a comparison function. The latter makes .sort() pretty powerful.

    var input = [28,32,21,11,8,2,14,32,64];
    
    var low2high = function ( a , b ) {
        return a > b;
    };
    
    var high2low = function ( a , b ) {
        return a < b;
    };
    
    var resultHigh2low = input.sort( high2low ); // [ 64,32,32,28,21,14,11,8,2 ];
    var resultLow2high = input.sort( low2high ); // [ 2,8,11,14,21,28,32,32,64 ];
    

    So if we want to use bubbleSort ( link provided by T.J. Crowder , see OP comments ) we can write the following:

    // javascript bubbleSort implementation
    var bubbleSort = function ( list , comparison ) {
        var swapped;
        var i;
        var val;
    
        list = [].concat( list ); // do not destroy original
        comparison = ( typeof comparison == "function" ) ? comparison : function(a,b){return a > b;}
    
        do {
            i = list.length;
            while ( --i ) {
               if ( i && comparison( list[ i ] , comparison[ i-1] ) ) {
                    val = list[ i ];
                    list[ i ] = list[ i - 1 ];
                    list[ i - 1] = val;
                    swapped = true;
               }
            }
        } while ( swapped );
        return list;
    }
    
    // using comparison functions from previous example.
    var resultHigh2low = bubbleSort( input , high2low ); // [ 64,32,32,28,21,14,11,8,2 ];
    var resultLow2high = bubbleSort( input , low2high ); // [ 2,8,11,14,21,28,32,32,64 ];
    

    Lets walk through it step by step:

    var bubbleSort = function ( list , comparison ) {
        ..code..
    }
    

    Our function accepts 2 parameters, first the array and 2nd an optional comparison function.

    var swapped;
    var i = list.length;
    var val;
    

    We store the list’s length under variable i, and declare 2 empty variables ( swapped and val ) we’re going to use later on.

    list = [].concat( list ); // do not destroy original
    

    We clone the list using [].concat( array ) and overwrite the local list variable leaving the original intact.

    comparison = ( typeof comparison == "function" ) ? comparison : function(a,b){return a > b;}
    

    We test the typeof the comparison argument, if it’s a function we use that one, otherwise we fall back on our own comparison function. Our fallback comparison function will return true if a is bigger than b.

    do {
        ..code..
    } while ( swapped );
    

    A do/while loop will run at least once, our swapped variable is currently undefined so it will be interpreted as falsy. If our comparison function returns true, a swap occurs and the swapped variable will be set to true, so it will loop again.

    while ( --i ) {
        ..code..
    }
    

    Here I loop from the list’s length downward, the -- operator is put before the i variable to ensure it is handled first before anything, i-- would go off after while evaluation causing erronous results since list[ list.length ] does not exist. I always do it this way (bad habbit perhaps), but if it confuses you, go for absolute transparancy.

    if ( i && comparison( list[ i ] , comparison[ i-1] ) ) {
        ..code..
    }
    

    First we check if i has a truthy value ( 0 evaluates to falsy ) and then we run the comparison function passing list[ i ] and list[ i - 1 ] as a and b parameters. If the comparison function returns true, we perform a swap.

    val = list[ i ];
    list[ i ] = list[ i - 1 ];
    list[ i - 1] = val;
    swapped = true;
    

    Here I perform the swap without using the .splice() method, it’s just an educated guess atm., but I figure direct assignments are faster then function calls. I use the val variable as a place holder. After the swap is done, I set swapped to true so our do/while loop will continue.

    return list;
    

    Well… return the result.

    I’ve excluded some checks, like what do we do when the list’s length is 0 and whatnot. Basically when writing helper functions, we also need to deal with error handling. Like for example throwing a TypeError when the passed comparison argument is not a function, ensuring the comparison method returns a boolean value and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Live Demo with visible code; http://jsfiddle.net/3eEgb/4/ The demo should be fairly self explanatory; I'm
I am writing code to migrate data from our live Access database to a
I have a simple jQuery function: $('#selectable1 span').live('mousedown', function() { var ff = $(this).css(font-family);
it is possible to bind Live('hover') and bind('contextmenu') on the same .class? This code
here is the live link: http://mrgsp.md:8080/a/Account/SignIn the main div (green one) doesn't take 100%
Please have a look at http://live.heritageartpapers.co.uk/catalogue.aspx and advise me on the following: Currently customers
I have the following code, which works fine on live site, but not on
I'm working off of some of Apple's sample code for mixing audio ( http://developer.apple.com/library/ios/#samplecode/MixerHost/Introduction/Intro.html
Assumption: live/production web app suppresses errors being shown to end-users. Suppose your tech support
We deployed a live, fresh, swanky site using preview 3 including rigorous stress testing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.