Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Pieter Bracke
Pieter Bracke
4,078 Points

Sorting integers in an array from low to high

Hi,

I am working on a project for which i need to sort integers which are stored in a variable from low to high and then based on the ranking from low to high change the font size from small to bigger for each higher integer.

//This my array: I am looking to store the first row of numbers from low to high.
var arrTags = [
                ["Javascript", 1634, 987],
                ["jQuery", 1111, 34],
                ["PHP", 1024,1122],
                ["Asp.Net", 977, 1005],
                ["Photoshop", 594, 789],
                ["XML", 40, 666],
                ["Access", 55, 77],
                ["Java", 278, 277],
                ["MySQL", 155, 122]
               ]

//This is my code i have so far which outputs the numbers in console but does not sort them yet.
for(var i = 0; i < arrTags.length; i++){
                            console.log(arrTags[i][1]);


                }

I don't want the exact solution but rather a push in the right direction on which method is best used to accomplish my goal, so I can figure out the rest myself.

Thx in advance Pieter Bracke

2 Answers

Abhishek Gangadhar
Abhishek Gangadhar
9,467 Points

Try storing the second value in another array.

var arrIndex= []
 for(var i = 0; i < arrTags.length; i++){
               arrIndex.push(arrTags[i][1]);
                }
arrIndex.sort();

for(var i = 0;i<arrTags.length;i++) {
              document.write((i+1)+" : "+arrTags.indexOf(arrIndex[i]));
}
Pieter Bracke
Pieter Bracke
4,078 Points

Okay, That was already a great help. For now I have come up with this code to try and loop through the whole new created array and then increase the font-size based on each iterations. However I seem to have made some kind of mistake somewhere because only my 3'rd array element seems to increase in size... further info: sSpan is an element span i dynamically created before and where in I have placed each element of the created array. Do you see how to fix my problem?

//Function to sort numbers from low to high
        function sortNumber(a,b){
            return a-b;
        }

        //Create new array to add the numbers
        var arrIndex = [];



        //For loop to sort from low to high
                for(var i = 0; i < arrTags.length; i++){
                            arrIndex.push(arrTags[i][1]);
                            arrIndex.sort(sortNumber);


                }
                   //for loop to iterate through each item in the array and increase the font size on each iteration
                for(var i = 0; i < arrIndex.length; i++){
                    if(arrIndex[i] > arrIndex[i - 1]){
                    sSpan.style.fontSize = (5 * i + 1) + "px";
                    }   
                }


        console.log(arrIndex);

        console.log(sSpan);