Js Sort

How to sort a list of objects in Javascript. I often do it wrong!

When you have a list in Javascript, you can call list.sort(…) with a function, but if your function is a boolean expression (I was used to this from Python) it won’t work!

Your function takes two items, I’ll call them left and right. A proper function should return a negative number if left < right, 0 if equal, and a positive number if left > right.

If you’re having issues sorting an array, it’s probably because your function doesn’t follow this pattern!

Read more on MDN - Array.sort