From 06685b14cfc10f97b3fd08333fe616b6443c7274 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 29 Jan 2021 10:52:14 -0500 Subject: [PATCH] ui: simplify compareOptions() with boolean arithmetic --- .../ui-components/AdvancedMultiSelect.js | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/monkey/monkey_island/cc/ui/src/components/ui-components/AdvancedMultiSelect.js b/monkey/monkey_island/cc/ui/src/components/ui-components/AdvancedMultiSelect.js index 6398e85db..5a6cc6fcd 100644 --- a/monkey/monkey_island/cc/ui/src/components/ui-components/AdvancedMultiSelect.js +++ b/monkey/monkey_island/cc/ui/src/components/ui-components/AdvancedMultiSelect.js @@ -51,23 +51,14 @@ class AdvancedMultiSelect extends React.Component { // Sort options alphabetically. "Unsafe" options float to the bottom" compareOptions = (a, b) => { - if (!this.isSafe(a.value) && this.isSafe(b.value)) { - return 1; + // Apparently, you can use additive operators with boolean types. Ultimately, + // the ToNumber() abstraction operation is called to convert the booleans to + // numbers: https://tc39.es/ecma262/#sec-tonumeric + if (this.isSafe(b.value) - this.isSafe(a.value) !== 0) { + return this.isSafe(b.value) - this.isSafe(a.value); } - if (this.isSafe(a.value) && !this.isSafe(b.value)) { - return -1; - } - - if (a.value < b.value) { - return -1 - } - - if (a.value > b.value) { - return 1 - } - - return 0; + return a.value.localeCompare(b.value); } onMasterCheckboxClick = () => {