Sort an Array

Hi. So I got two arrays where count[i] is the amount of times letter[i] appears.

let letters = ['a', 'e', 'i', 'o', 'u'];
let count = [13, 24, 9, 11, 7];

Specifically, ‘a’ appears 13 times, ‘e’ 24 times, etc.
What I’m trying to do is to sort the count in a descending order an to do the same sort in letters array, in order to keep the relation. So I should get something like this:

letters = ['e', 'a', 'o', 'i', 'u'];
count = [24, 13, 11, 9, 7];

I’ve been trying to do a bubble algorithm but I haven’t been able to write it in code, however this way is not really time efficient due the large array I got (code is simplified), is still better than nothing. Anyway, I’d appreciate any help in developing the algorithm or an efficient way to do this.

All this is because I need to get the 3 letters that appear more often, so then I’d do something like:

$w('#text1').text = count[1].toString();    // 24
$w('#text2').text = letter[1];                // e
$w('#text3').text = count[2].toString();    // 13
$w('#text4').text = letter[2];                // a
$w('#text5').text = count[3].toString();    // 9
$w('#text6').text = letter[3];                // o

You can try:

let letters = ['a', 'e', 'i', 'o', 'u'];
let count = [13, 24, 9, 11, 7];
let lettersData = [];
for (let i = 0; i < letters.length; i++){
  lettersData.push({"letter": letters[i], "count": count[i]})
}
lettersData.sort(function (a, b) {
    return b.count - a.count;
});
$w.onReady(() => {
  $w('#text1').text = lettersData[0].count.toString();    // 24
  $w('#text2').text = lettersData[0].letter;                // e
  //etc...
})

This worked perfectly, thank you!

@benguria2 you’re welcome :slight_smile: