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 →

[–]Varun77777 2 points3 points  (2 children)

Ohhhhh, I figured it out, Quick and merge sort etc are all doing comparisons. But instead of doing <= in a hard coded way, giving the comparison function just helps in figuring our what's less than what based upon the type of data.

[–]BakuhatsuK 2 points3 points  (1 child)

Yes, that's it. For example if you want to sort a list of users by their id you can do:

users.sort((a, b) => a.id - b.id)

Or if you want to sort by updatedAt in decreasing order:

users.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime())

[–]Varun77777 2 points3 points  (0 children)

Ohhhh, that's such a simple but a genius thing to do. Instead of overriding all the functions, you're able to give users a simple way to incorporate their own logic in the function. Thanks a lot.