5 ms·
Good point, for those who need to implement a slick compareFunction for numbers, Math.sign is great: [min, num, max].sort((a, b) => Math.sign(a - b))[1] An
by yesbabyyes 6y ago
Good point, for those who need to implement a slick compareFunction for numbers, Math.sign is great:
[min, num, max].sort((a, b) => Math.sign(a - b))[1]
An entirely different alternative is poor man's match:
switch (true) {
case num > max: return max;
case num < min: return min;
default: return num;
}
- recursive 6y agoWhy sign though? I always write my sorts like this. .sort((a, b) => a - b)