0

I get a function's results in an array of objects:

let options = [
  {
    "aCol": 0,
    "aRow": 10,
    "bCol": 12,
    "bRow": 4,
    "cCol": 5,
    "cRow": 1,
  },
  {
    "aCol": 4,
    "aRow": 10,
    "bCol": 3,
    "bRow": 4,
    "cCol": 1,
    "cRow": 1,
  },
  {
    "aCol": 4,
    "aRow": 10,
    "bCol": 3,
    "bRow": 0,
    "cCol": 0,
    "cRow": 1,
  }
];

that I want to inspect the value pairs, i.e.:

aCol
aRow

and sort them in ascending order by the number of zeroes am amongst the three pairs. The result should be:

let options = [
  {
    "aCol": 4,
    "aRow": 10,
    "bCol": 3,
    "bRow": 0, // b contains a zero
    "cCol": 0, // c contains a zero
    "cRow": 1,
  },
  {
    "aCol": 0, // a contains a zero
    "aRow": 0,
    "bCol": 12,
    "bRow": 4,
    "cCol": 5,
    "cRow": 1,
  },
  {
    "aCol": 4,  // no zeroes in this object in any of the pairs
    "aRow": 10,
    "bCol": 3,
    "bRow": 4,
    "cCol": 1,
    "cRow": 1,
  },
];

I've been messing around leveraging the zeroes and with shortcutting like this:

options.sort((x,y) => 
  (x.aCol * x.aRow < y.aCol * y.aRow) ||
  (x.bCol * x.bRow < y.bCol * y.bRow) ||
  (x.cCol * x.cRow < y.cCol * y.cRow) 
) 

console.log(...options); 
3
  • And if the number of zeroed coordinates is the same for both objects? Commented Oct 16, 2020 at 22:25
  • @raina77ow I do not need to distinguish amongst the two, in that case. I am trying to order them into as many as three buckets: 1) those with two pairs that contain a zero, 2) those with one pair that contain a zero and 3) those that have no zero. The array is used sequentially for displaying the "options." There is the possibility that none or all of the objects have similar zero-containing pairings. That make sense? Commented Oct 16, 2020 at 22:30
  • 1
    Step 1: make it work, and then only once you got that, step 2: make it concise. You look quite capable of writing out the code you need, so just do that first, without trying to write clever shortcuts, and then once that works, start to condense the code. You don't seem to need our help here? Commented Oct 16, 2020 at 22:34

2 Answers 2

1
let options = [
  {
    "aCol": 1,
    "aRow": 10,
    "bCol": 12,
    "bRow": 4,
    "cCol": 0,
    "cRow": 1,
  },
  {
    "aCol": 4,
    "aRow": 10,
    "bCol": 3,
    "bRow": 4,
    "cCol": 1,
    "cRow": 1,
  },
  {
    "aCol": 4,
    "aRow": 10,
    "bCol": 3,
    "bRow": 0,
    "cCol": 0,
    "cRow": 1,
  }
];


const sorted = options.sort((x, y) => {
   const xZeros = +!(x.aCol * x.aRow) + +!(x.bCol * x.bRow) + +!(x.cCol * x.cRow);
   const yZeros = +!(y.aCol * y.aRow) + +!(y.bCol * y.bRow) + +!(y.cCol * y.cRow);
   return yZeros - xZeros
})

console.log(sorted)

works for me test

+!(x.aCol * x.aRow) 

so if multiplication is zero then this invers it to "true" and then "+" transform it to 1;

if multiplication is something more than 0 it invers it to false and then this is 0

Sign up to request clarification or add additional context in comments.

Comments

0

function compareColAndRowZeroCounts(a, b) {

  const productZeroCountA = [
    (a.aCol * a.aRow), (a.bCol * a.bRow), (a.cCol * a.cRow)
  ].filter(num => !num).length;

  const productZeroCountB = [
    (b.aCol * b.aRow), (b.bCol * b.bRow), (b.cCol * b.cRow)
  ].filter(num => !num).length;

  const totalZeroCountA = Object.values(a).filter(num => !num).length;
  const totalZeroCountB = Object.values(b).filter(num => !num).length;

  // ascending order
  return ((productZeroCountA > productZeroCountB) && -1)
    || ((productZeroCountA < productZeroCountB) && 1)
    || ((totalZeroCountA > totalZeroCountB) && -1)
    || ((totalZeroCountA < totalZeroCountB) && 1)
    || 0;
}

const options = [{
  "aCol": 0,
  "aRow": 0,
  "bCol": 12,
  "bRow": 4,
  "cCol": 5,
  "cRow": 1,
}, {
  "aCol": 4,
  "aRow": 10,
  "bCol": 3,
  "bRow": 4,
  "cCol": 1,
  "cRow": 1,
}, {
  "aCol": 4,
  "aRow": 10,
  "bCol": 3,
  "bRow": 0,
  "cCol": 0,
  "cRow": 1,
}];

console.log(
  'options.sort(compareColAndRowZeroCounts)',
  options.sort(compareColAndRowZeroCounts)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.