I am trying to make a PEN that takes names and ratings, sorts the 2 arrays based on the ratings, using Bubblesort and displays the sorted arrays. But it doesn’t seem to work for numbers greater or equal than 10 (tried 9.99 and it works but not with 10.001 or 10). Here’s the code:
for (var i = numberOfCountries -1; i>=0; i-- ){
for (var j = 0; j <= i; j++){
if (ratings[j-1] < ratings[j]){
var temp = ratings[j-1];
ratings[j-1] = ratings[j];
ratings[j] = temp;
temp = countries[j-1];
countries[j-1] = countries[j];
countries[j] = temp;
}
}
}
I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
There is nothing wrong with your sort. The problem is in the ratings array. Put a console.log statement for the ratings array after the for loop of prompts and you will see that ratings is an array of string items. It needs to be an array of numeric items.