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 JavaScript Foundations Arrays Methods: Part 2

sort()

Hello!

In the lecture video, the code below outputs the following [0, 10, 100, 3, 32, 4, 44, 44], because it is looking at the array values like strings, but 'z'ero, 't'en, 'h'undred,, and 't' hree, z does not come before t, what I am missing here. Yes, I know this is not the way to compare numbers in an array. I just would like to know what the instructor meant by 'it is sorting it by String'. If I understand what is really happening I think can remember the reason I should not perform a sort() on an array with numbers. Cheers!

<!DOCTYPE html> <html lang="en"> <head> <title> JavaScript Foundations: Arrays</title> <style> html { background: #FAFAFA; font-family: sans-serif; } </style> </head> <body> <h1>JavaScript Foundations</h1> <h2>Arrays: Methods Part 1</h2> <script> var myA = [10,44,32, 100, 0, 44, 3,4]; myA.sort();

</script>

</body> </html>

Hi again orange sky :)

Try to wrap your code with 3 backticks (```) on the line before and after. It will help to keep formatting. You can find a good guide of inserting code into a question in the video "Tips for asking questions". This video placed on the right sidebar of this forum page --------->

7 Answers

When sorting as strings, the number 0 becomes the string "0" not "zero"

So the original number based array is going to be sorted as if it was ["10", "44", "32", "100", "0", "44", "3", "4"]

Think of the digits as characters. "4" would come before "44" in the dictionary the same way as "a" would come before "aa"

By default, the sort method will sort the array lexicographically or dictionary order.

This same video does cover how to sort the numbers as numbers rather than strings by using the compare function. So you can sort an array with numbers.

Correction: I think it would be more correct to say that the default sort converts the elements to strings and then sorts them based on Unicode point order.

From Array.prototype.sort():

"If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order. For example, "Cherry" comes before "banana". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order."

From code sample-
"// In Unicode, numbers come before upper case letters, which come before lower case letters."

Hello Alexander :)

That is right! One of the leaders mentionned that to me. Ok, bear with me :) I don't catch on to things quickly, but I do get it eventually:) Thanks for the reminder, and I will have a look at the video on the right, too. Cheers!!!!

Hello Jason and Albert,

Ok, thanks Jason for pointing that the computer see 0 as the character '0' and not spelled out as Zero.

And thanks Albert for taking it a little further by telling me that charaters are stored and ordered based on their binary value. Now it all makes since as to why an array of number is seen as an array of characters.

Thanks!

got it!!

Hello all,

I am seeing all kinds of possible answers on how an array converts its elements into strings, but I cant figure out which is the correct one.

I wonder if this explantion here by Jason is the one to go with. It seems plausible ?

Jason's answer:

****For example, "Cherry" comes before "banana". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order."

From code sample- "// In Unicode, numbers come before upper case letters, which come before lower case letters."******

Hi!

I suppose the best answer is a reference to the documentation, because people may be wrong — documentation unlikely.

Jason Anello have already given a link to MDN - Array.prototype.sort() and his answer corresponds to it.

thanks Alexander, I read the posts and I tried to make sense of those, but how that I have opened up that link. I see what the correct answer is.

Thanks Jason!

Albert Evangelista
Albert Evangelista
27,689 Points

I think it stems from the fact that characters like numbers or letters have equivalents which enables the computer to interpret them so the computer can work on them. It may look like a "0" to you but in a computer its 0000000. An "A" to a computer is 0100 0001. Its called ASCII. You can look it up at an ASCII table and you will see why.

Hi Albert,

The character 'A' is decimal 65 but the character '0' (zero) is not decimal 0 in the ascii table. It's decimal 48. Decimal 0 is for the null character.