switch (true) {
case cellA > cellB: return 1;
case cellA < cellB: return -1;
case cellA === cellB: return 0;
}
Source: https://htmldom.dev/sort-a-table-by-clicking-its-headers switch (true) {
case cellA > cellB: return 1;
case cellA < cellB: return -1;
case cellA === cellB: return 0;
}
Source: https://htmldom.dev/sort-a-table-by-clicking-its-headers if (cellA > cellB) return 1;
if (cellA < cellB) return -1;
if (cellA === cellB) return 0;
Why is the switch better?
Or even (less explicit, but shorter code): if (cellA === cellB) return 0;
return cellA > cellB ? 1 : -1; return (cellA === cellB) ? 0 : (cellA > cellB ? 1 : -1); return (cellA > cellB) * 1 + (cellA < cellB) * -1;
More: return Math.sign(cellA - cellB); return (cellA > cellB) - (cellA < cellB); 'ana' > 'Bob'
'2' > '123'
You usually want to clearly define the order or pre-process the strings in some way (trim them, same casing, etc).Or indeed drop the <, > based approach and use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Or: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
return (
(cellA === cellB) ? 0 :
(cellA > cellB) ? 1 :
-1
);
and it's an expression, so i immediately know it won't do any weird control flow. i'd prefer "if/then/else" vs "?/:" but it's not badI find a better indendation style for this is more like
condition
? truthy
: falsyyeah, ternary-if is busted in php :/ (far from the only thing that's busted there though...) i'm actually doing some PHP work right now, and never chain ifs to avoid this exact thing.
Your last version doesn't tell the reader what your intention is at all, and they need to work out what you're trying to do here. It's a lot less readable. Unless you're desperate for those bytes, it'd be better to use the switch statement for this case.
[0]: This gets a little weird in languages like JS where switch statements fall through to the next one if you don't break or return, but generally it still holds true.
if (cellA > cellB) return 1;
if (cellA < cellB) return -1;
if (cellA === cellB) return 0;
throw new Error();The correct is return a<b?-1: (a>b? 1 :0)
var a="a", b=1; a<b ? -1 : (a>b ? 1 : 0); // 0
The switch and if statements would return undefined in these cases. Or others threw an error if none of the conditions matched, that would work too.
I don't know about V8, but a lot of compilers would have a harder time optimizing this because of the strange structure. For mature compilers and naive (i.e. not-yet-profiled) projects, it's better to write what you mean and let the compiler optimize it.
So yes, I do find this more readable. It makes it clear that only one branch will get executed.
return cellA.localeCompare(cellB);
Because cellA and cellB are always strings: const cellA = rowA.querySelectorAll('td')[index].innerHTML;
If using a framework, you would generally have typed JSON data, which is better. For example, makes number columns sort correctly (example code above sorts ‘10’ before ‘9’).Dealing with cells of tables is one area where code optimisations are needed if you care about performance and have a large table. There are multiple obvious problems e.g. looping over calls to querySelectorAll.
That switch(true) statement is not what I would expect from a more experienced developer. It is show-off code that looks cute and works, but the compromises are not worth it (statement order is not obvious, if you make a mistake and two cases are true then do you know which wins, it could easily deoptimise the JIT compiler because it is doing something uncommon, I would worry how debuggers and code compressors would handle more complex cases, and understandability is poor for new devs IMHO).
function test() {
switch (true) {
case console.log('asdf') === undefined: return 1;
case console.log('qwer') === undefined: return 2;
}
}
test();