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

Sort variables from highest to lowest

I'm trying to sort a group variables by their value (highest to lowest). As you can see I'm a massive beginner with javascript. I don't even think it's possible to group variables inside a variable?! Any help would be much appreciated! :D

<script>

var score = [jobA , jobB , jobC , jobD , jobE , jobG , jobI , jobK , jobL , jobJ , jobH, jobF, jobM]; document.getElementById("result-1").innerHTML = score;

$(document).ready(function(){

    $("#myresults").hide();
    $("#joblinks").hide();
});

    function changeText(){
   if ((personalSkillsCheckboxCount >= 3)  && (workSkillsCheckboxCount >= 3)) {
      document.getElementById("result-1").innerHTML = ( ("Allied Health Assistant " + jobA) + ("Physical Activity Co-ordinator /recreation officer " + jobB) + ("Health Promotion Officer " + jobC) + ("Exercise Physiologist " + jobD) + ("Sports Development Co-ordinator " + jobE) + ("Wellbeing presenters " + jobF) + ("Research/academia (honours pathway) " + jobG) + ("Sport administrator " + jobH) + ("Sport marketer / event manager  " + jobI) + ("Sport facility operations manager " + jobJ) + ("Project Manager " + jobK) + ("Project Officer " + jobL) + ("Recreation Officer in a Womens Prison " + jobM) );

      document.getElementById("instructions").innerHTML = ("The jobs with the highest score relate to the job best suited to you based on your career interests, personal attributes and work-related skills. Which job did you score highest in? Now you can explore these jobs in more detail by clicking on the job links below.");

      score.sort(function(a, b){return b-a});
      document.getElementById("result-1").innerHTML = score;

      $("#workskills").slideUp();
      $("#personalskills").slideUp();
      $("#myresults").show();
      $("#joblinks").slideDown();
      ("result-1").sort(function(a, b){return b-a});
      document.getElementById("result-1").innerHTML = ("result-1");
        }
      }


  }
    else {
        alert("Please check at least 3 skills in each column")
 }

} </script>

1 Answer

maybe try http://www.w3schools.com/jsref/jsref_sort.asp . If your objects are a little complicated, then you might been to rewrite the comparison function to consume your object's structure. An example i wrote recently, is schools.sort(function(a, b){return a.avg-b.avg }); It's basically the same as a normal numeric sort, but knows about the objects it's using (particularly that they have an avg attribute).