2

var data = {
  "input": [{
      "countA": 1,
      "countB": 10
    },
    {
      "countA": 15,
      "countB": 13
    },
    {
      "countA": 26,
      "countB": 24
    },
    {
      "countA": 6,
      "countB": 25
    },
    {
      "countA": 15,
      "countB": 20
    }
  ]
};

var sorted = data.input.sort(function(a, b) {
  return a['countB'] < b['countB'] ? 1 : -1;
});

console.log(sorted);

The outcome after the first sorting should be after another sorting:

[
    {
        "countA": 6,
        "countB": 25
    },
    {
        "countA": 15,
        "countB": 20
    },
    {
        "countA": 1,
        "countB": 10
    }
    {
        "countA": 26,
        "countB": 24
    },
    {
        "countA": 15,
        "countB": 13
    }
]

So, it should be the highest of "countB" and then descending as long as "countB" is higher than "countA". So far I tried multiple ways, but there's no outcome so far.

Thanks for any help!

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Hols1990
  • 35
  • 2
  • 2
    There is no obvious logic behind your output – mplungjan Aug 30 '21 at 17:18
  • 1
    *"... as long as "countB" is higher than "countA"."* which `"countA"` and `"countB"`? `a`'s or `b`'s? And then, what should be the comparison? – Cid Aug 30 '21 at 17:20
  • 1
    Telling us your use case or your exercise statement will help us to understand what you're trying to achieve – Cid Aug 30 '21 at 17:24
  • 1
    The integer of "countB" always has to be higher than the integer of "countA", that's the idea behind my post. So, whenever "countA" is higher than "countB", "countA" should be not relevant for the sorting. – Hols1990 Aug 30 '21 at 17:25
  • What to do with the values then when `"countb" < "countA"`? should they be randomly placed at the end of the array? – Cid Aug 30 '21 at 17:25
  • 1
    @Cid: That's exactly the question...That's why I think that another sorting after the first sorting as shown "return a['countB'] < b['countB'] ? 1 : -1;" is needed. But I can't find the solution for it. – Hols1990 Aug 30 '21 at 17:27
  • Does this answer your question? [How to sort an array of objects by multiple fields?](https://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields) – pilchard Aug 31 '21 at 09:28

2 Answers2

1

You could sort by the result of the comparison of countB > countA and then by the value of countB.

const
    data = [{ countA: 1, countB: 10 }, { countA: 15, countB: 13 }, { countA: 26, countB: 24 }, { countA: 6, countB: 25 }, { countA: 15, countB: 20 }];

data.sort((a, b) =>
    (b.countB > b.countA) - (a.countB > a.countA) ||
    b.countB - a.countB
);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can first extract the values of the array when "countb" >= "countA", sort that array then add the remaining values at the end (note that for order is kept for when "countb" < "countA") :

var data = {
  "input": [{
      "countA": 1,
      "countB": 10
    },
    {
      "countA": 15,
      "countB": 13
    },
    {
      "countA": 26,
      "countB": 24
    },
    {
      "countA": 6,
      "countB": 25
    },
    {
      "countA": 15,
      "countB": 20
    }
  ]
};

const ElementsToSort = data.input.filter(elem => elem.countA <= elem.countB);
const RemainingElements = data.input.filter(elem => ElementsToSort.indexOf(elem) < 0);

// sort as you did
const PartiallySorted = ElementsToSort.sort(function(a, b) {
  return a['countB'] < b['countB']
          ? 1
          : a['countB'] > b['countB']
            ? -1
            : 0;
});


//add the remaining values
const sorted = PartiallySorted.concat(RemainingElements);
console.log(sorted);
Cid
  • 14,968
  • 4
  • 30
  • 45