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
Daniel Hunter
7,349 PointsJavascript Methods 2 - Task 2
Sort the 'saying2' array, on about line 19, so that the words are listed in length order. The shortest first. Use the 'length' property on the strings in the sort function.
<script>
var saying1 = ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"];
var saying2 = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog's", "back"];
saying1.reverse();
saying2.sort(function ());
I've got this one started, just not sure how to include the .length part.
3 Answers
Peter Szerzo
22,661 PointsHey Daniel,
Have a look at this code, which should answer your question:
saying2.sort( function(a, b) { return (a.length - b.length); });
What you are specifying in the parenthesis is the 'rule' which your program should follow when comparing two elements in your array - based on whether the return value is positive or negative. How does a comparison come into play? Sorting is very closely related to comparing. The sorting mechanism that is built into JavaScript swaps around elements in your array if they are in the wrong order, that is, if they don't compare as they should (if there is a longer string before a shorter one).
Let me know if you'd like further clarification.
Peter
geoffrey
28,736 PointsTry this
var saying2 = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog's", "back"];
saying2.sort(function(a,b){
return a.length -b.length;
});
// this returns ["The", "fox", "the", "over", "lazy", "back", "quick", "brown", "dog's", "jumped"]
I coudln't remember the answser, so I checked the mozilla developer network to find the right answser.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Daniel Hunter
7,349 PointsThanks for sending this website, super helpful!
Brian van Vlymen
12,637 PointsHello, @Peter Szerzo, @geoffrey
Can you please explained to me better example please. One mostly I dont really understand why should we put "return a - b" however I completely understand ASCII or Unicode how the sort or length method work. let don't worry about the "length" method.
var saying2= [5,4,3,2,1,0]
If saying2.sort(function(a, b){
return a - b;
});
console.log (saying2);
the answer would be 0,1,2,3,4,5 because of return is less than 0, sort a to a lower index than b, i.e. a comes first?
I am confused why required put "a - b" to make it work?
Brian van Vlymen
12,637 PointsPeter Szerzo
22,661 PointsIf a is lower than b, than (a - b) is negative, and this is "negativity" is what JavaScript needs to know. Think of it as translation between human languages: just suppose for a moment that within the sort method, "a is lower than b" is a phrase in Japanese and "a - b is negative" is a phrase in German. In this case, from the way it is implemented, JavaScript only speaks German.
That said, Chrome will let you sort by returning a true or false value in the function, but in that case, Safari will sort the other way (or at least the latest versions at that time). Not the most predictable behavior..
Daniel Hunter
7,349 PointsDaniel Hunter
7,349 PointsOk great thanks!