I'm trying to sort numbers from user input (.prompt) from largest to smallest using the insertion sort method. I'm having difficulty understanding how to apply this method in my html code. Any advice is greatly appreciated!
-
you can google this. here is one result https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=64&lngWId=14 – Tareq Oct 03 '17 at 13:28
3 Answers
The only difference according to me might be with interface part as prompt take input as a string where you can take input as csv then split on commas and get an array of substrings, then parseInt and use in sorting as usual.

- 1
- 4
Note that this assumes the user is only going to input numbers. You will want to modify this code to account for users entering all kinds of data.
Updated Answer
This is self-explanatory if you're familiar with map
and parseFloat
. parseFloat
just converts a string to a floating point number (1.5, 6.004, etc). map
calls a callback function on each element of an array, and returns an array that contains the results. In this code, map
is doing parseFloat
on each element of the array and returning the result.
Oh, and one more thing that isn't obvious; sort
sorts according to string Unicode code points (see here for more info). Therefore, 10 comes before 2 because 1 comes before 2. That's why we need .sort((a,b) => (a-b))
, which I got from a comment on this answer.
const numberArray = prompt('Enter several numbers with a space between each').split(' ');
numberArray.map(element => parseFloat(element)).sort((a,b) => (a-b)).reverse();
First answer, but sorts 6 as greater than 10 because 6 comes before 1 in ASCII I think.
const numberArray = prompt('Enter several numbers with a space between each').split(' ');
numberArray.map(element => parseFloat(element));
function insertionSort(array) {
for (let i = 0; i < array.length; i += 1) {
const temp = array[i];
let j = i - 1;
while (j >= 0 && array[j] > temp) {
array[j + 1] = array[j];
j -= 1;
}
array[j + 1] = temp;
}
return array.reverse();
}
insertionSort(numberArray);
I got the insertion sort algorithm from Benoit Vallon's blog

- 514
- 5
- 21
As mentioned in the previous answer.. It is the same as you would do with any other language.
You need however to update your question to reflect exactly what have you tried and ask something more acurate.
Nevertheless, here is a simple sample of how you can achieve this. Take note that I did not do any type validation or or conversion. So it's on you to make sure the user inputs a number
var toSort=[];
$('#sort').on('click',function(){
var result= insertionSort(toSort);
$('p').html(result.toString());
});
$('#prompt').on('click',function(){
toSort.push(prompt("Enter a number",0));
});
/* from http://blog.benoitvallon.com/sorting-algorithms-in-javascript/the-insertion-sort-algorithm/ */
function insertionSort(array) {
for(var i = 0; i < array.length; i++) {
var temp = array[i];
var j = i - 1;
while (j >= 0 && array[j] > temp) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
return array;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="prompt">Prompt</button>
<button id="sort">Sort</button>
<h1>End result:</h1>
<p></p>

- 340
- 5
- 14