3

The question is:

"My friend John and I are members of the "Fat to Fit Club (FFC)". John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest.

I am the one who establishes the list so I told him: "Don't worry any more, I will modify the order of the list". It was decided to attribute a "weight" to numbers. The weight of a number will be from now on the sum of its digits.

For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will come before 99. Given a string with the weights of FFC members in normal order can you give this string ordered by "weights" of these numbers?"

Example

"56 65 74 100 99 68 86 180 90" ordered by numbers weights becomes: "100 180 90 56 65 74 68 86 99" When two numbers have the same "weight", let us class them as if they were strings (alphabetical ordering) and not numbers: 100 is before 180 because its "weight" (1) is less than the one of 180 (9) and 180 is before 90 since, having the same "weight" (9), it comes before as a string.

All numbers in the list are positive numbers and the list can be empty.

This is the code I have so far:

function sumOfParts(num) {
  return num.split('').reduce((a, b) => parseInt(a) + parseInt(b), 0)
} 

function orderWeight(string) {
  return string.split(' ').sort().sort((a,b) => sumOfParts(a) - sumOfParts(b)).join(' ')
}

The code words on strings even with two consecutive numbers with the same value but when 3+ numbers with the same sum are added it starts to break.... Here are some strings that broke it:

Expected: '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984', instead got: '112 14 170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984'

Expected: '200 113 41 114 25 52 109 155 83 76 161330 59 450231 274111 93131 440830 432353 274292 320986 371567 29858', instead got: '200 41 113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830 432353 274292 320986 371567 29858'

Been stuck on this one longer then I would like to admit lol

Thanks

Nadav Julius
  • 301
  • 2
  • 16

1 Answers1

6

You need a single sort and a sorting by string for same sums.

function sumOfParts(num) {
    return num.split('').reduce((a, b) => a + +b, 0)
} 

function orderWeight(string) {
    return string
        .split(' ')
        .sort((a, b) => sumOfParts(a) - sumOfParts(b) || a > b || -(a < b))
        .join(' ');
}

console.log('out', orderWeight('112 14 170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984'));
console.log('exp', '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984');


console.log('out', orderWeight('200 41 113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830 432353 274292 320986 371567 29858'));
console.log('exp', '200 113 41 114 25 52 109 155 83 76 161330 59 450231 274111 93131 440830 432353 274292 320986 371567 29858');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Does this "a + +b" cover the strings into Ints automatically? Do we not need a parseInt or Number(a) – mrahma04 Dec 02 '21 at 14:31
  • @mrahma04, the [unary plus `+`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus) converts the value to a number. – Nina Scholz Dec 02 '21 at 14:34
  • Thank you so much! This is such an elegant solution. Is '||' the OR operator in the sort method? Trying to understand the logic of the sort method...I get the first part...sumOfParts(a) - sumOfParts(b)...how is the rest evaluated...|| a > b || -(a < b)) – mrahma04 Dec 02 '21 at 14:42
  • Also do you not need to convert 'a' as well to a number...as in...+a + +b – mrahma04 Dec 02 '21 at 14:46
  • 2
    @mrahma04the expression is separated by [logical OR `||`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR) and if a part between returns zero or any other falsy value the next part is evaluated. the part `a > b || -(a < b)` works for strings and numbers, but if string, it sorts by string and with numbers it sorts by numbers. in short it returns `true`, if the `a` is smaller than `b` and if greater, it negates `true` and returns `-1`. `reduce` starts with zero and therefore `a` is always a number. – Nina Scholz Dec 02 '21 at 14:52
  • @mrahma04, `|| (a > b ? 1 : -1)` also works instead of `|| a > b || -(a < b)`. This part is for sorting the strings (of numbers) alphabetically. (These 2 variations are not exactly the same, because they return different things when `a === b`. However, this does not matter for this problem since the resulting array will look the same.) – hb20007 May 20 '22 at 18:10