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 9001305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:25:08+00:00 2026-06-16T00:25:08+00:00

I am trying to use a selection sort for javascript but it does not

  • 0

I am trying to use a selection sort for javascript but it does not seem to work, can somebody help me please?
I create a function to sort an array,
then I get the value from the text box, and store it in an array named inputString
then I split the array by comma, and store it in an array named inputNumbers
If in my text box exist non number, an error will be displayed otherwise display the sorted value.

function sortNow(form) {
    var nanExists = false;
    var inputString = document.getElementById("numberID").value;
    var inputNumbers = inputString.split(",");
    for (var a = 0; a < inputNumbers.length; a++) {
        inputNumbers[a] = parseInt(inputNumbers[a], 10);
        if (isNaN(inputNumbers[a])) {
            nanExists = true;
            break;
        }
    }

    inputNumbers = selectionSort(inputNumbers); //sort the array inputNumbers

    if (nanExists)
        form.answers.value = "Invalid Input";
    else
    {
        for(var b=0; b < inputNumbers.length; b++)
        {
            form.answers.value += inputNumbers[b];
        }
    }
    }
/* function to sort an array */
    function selectionSort(inputArray) {
    for(var i=0; i<inputArray.length; i++)
    {
        var currentMin = inputArray[i];
        var currentMinIndex = i;    
        for(var j=i+1; j<inputArray.length; j++)
        {
            if(currentMin > inputArray[j])
            {
                currentMin = inputArray[j];
                currentMinIndex = j;
            }
            if(currentMinIndex != i)
            {
                inputArray[currentMinIndex] = inputArray[i];
                inputArray[i] = currentMin;
            }
        }
    }
    return inputArray;
}
  • 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-06-16T00:25:08+00:00Added an answer on June 16, 2026 at 12:25 am

    In your code please move the following code segment from inner loop (j) to outer loop (i).

            if(currentMinIndex != i)
            {
                inputArray[currentMinIndex] = inputArray[i];
                inputArray[i] = currentMin;
            }
    

    See the demo

    Please sort the inputArray like this:

    inputArray.sort(function(a,b) {
      return (a > b) ? 1 : ((a == b) ? 0 : -1);
    });
    

    You need to supply a custom sort function to Array.sort because Array.sort() sorts array lexicographically (in dictionary order) according to the string conversion of each element.

    See a working demo here

    Read up the MDN:Array.sort

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

Sidebar

Related Questions

I'm trying to use a custom ::selection effect in Chrome but getting this bad
I am trying to load Skyscanner API dynamically but it doesn't seem to work.
I am trying to use css3's :not() selector to select certain elements that do
I'm having some issues with a report I'm trying to create for TFS work
I'm trying to use IVsMonitorSelection to know when the user is selection a control
I a trying to build an application where i can use UICollectionviewcontroller & NSFetchresultcontroller
I'm trying to sort an array of NSStrings but i'm unsure how to actually
I am trying to use a solution for a cascading dropdownlist (the selection in
I am testing the different sort methods (Selection, bubble insertion) and I am trying
I trying to sort some data to use sortedArrayUsingDescriptors: method in an NSArray class.

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.