Node.js array.sort with custom method causes RangeError: Max Call Stack Size Exceeded

So, I have an array of 100,000+ items, and this line causes it to blow the call stack:

@sortedList.sort (a, b) ->
  return if a.value > b.value then -1 else 1

I'm about to go implement a custom sort of some variety (suggestions, anyone?) to work around the issue, but I just wanted to make sure that I'm not doing something blatantly stupid that's causing it to blow up.

What happens if a.value == b.value? A sort comparison function should return zero if the items are the same:

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b.
  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.
  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a.

So you want something more like this:

if a.value > b.value
  -1
else if a.value < b.value
   1
else
   0

The missing a.value == b.value branch could cause sort to lose its mind and make a mess all over the place; no == branch is saying that a goes before b and b goes before a, you can't expect a sensible result from that.