javascriptsortinglexicographic

Weight for weight from CodeWars


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


Solution

  • 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');