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;
}
5
  • refer this stackoverflow.com/questions/9887542/… Commented Dec 17, 2012 at 3:46
  • I am not familiar with jquery so i made it in javascript, and in my case, it is different from other one. Commented Dec 17, 2012 at 4:05
  • What is the specific problem? Are you getting a javascript error? If so, what is the error? Is it just not sorting properly, but no errors? Also, why don't you just use javascript's Array.sort()? Commented Dec 17, 2012 at 4:16
  • It is not sorting properly. Can you please show me why my sort function work properly? Thank you for the info about Array.sort() Commented Dec 17, 2012 at 4:26
  • @JackyNguyen: You need to write a custom sort function because Array.sort() sorts array lexicographically (in dictionary order) according to the string conversion of each element. See the answer below. Commented Dec 17, 2012 at 4:45

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@raghaw: I know of Array.sort() but can you please take a look at my sort function above to see why it does not work properly. I use the same function like that and it works in Java but when I do the same for Javascript, it does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.