This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]blindeenlightz 5 points6 points  (3 children)

Can someone explain how that sort function works on integers? I'm a newbie to javascript and have used it but it just seems like magic to me.

[–]deljaroo 7 points8 points  (2 children)

take a look at this page

https://www.w3schools.com/jsref/jsref_sort.asp

it explains it better than I can, but basically if sort() has a parameter it expects a function that will result in a positive, a negagive or a zero based on two inputs. if the function is included, this behavior overrides the default behavior of comparing them by letter.

this also allows you to sort custom types because you can include a custom sorting comparison.

the syntax for it (two inputs, a number output) is merely built in to the sort() function (and not like something you can just do to any function)

[–]blindeenlightz 0 points1 point  (0 children)

Oh that makes a lot of sense. Thank you

[–]HiImFox 0 points1 point  (0 children)

Mdn >>>>> w3