Criteria for sorting numbers as strings

I know a similar question exists here Javascript string conversion and array sorting , but I need help with my version of the solution.

Problem: The weight of a number is the sum of its digits. For example, 99 will have a "weight" of 18, 100 will have a "weight" of 1, so 100 will go up to 99 in the list. If the string with weights is in normal order, you can return this string ordered by these weights.

Example: "56 65 74 100 99 68 86 180 90", ordered by weights, becomes: "100 180 90 56 65 74 68 86 99" FFC members When two numbers have the same "weight", let's classify them as if they were strings, not numbers: 100 to 180 because his "weight" (1) is less than one of 180 (9) and 180 - up to 90 because he has the same "weight" (9) as he preceded as a string.

Question: The only part I am missing is how to do the last part, so for example 180 would fall to 207 and 90, similarly 207 to 45, etc.

var arr = [56, 65, 74, 100, 99, 207, 45, 68, 86, 180, 90];
arr.sort(function(a, b) {
  var A = ("" + a).split("").reduce(function(c, d) {
    return Number(c) + Number(d);
  });
  var B = ("" + b).split("").reduce(function(e, f) {
    return Number(e) + Number(f);
  });
  if (A > B) {
    return A - B;
  } else if (A == B) {

    //I ASSUME I SHOULD WRITE SOMETHING HERE

  }
});

console.log(arr);
      

Run codeHide result


+3


source to share


3 answers


You can add a sort criterion with a comparison result with String#localeCompare

.



function weight(v) {
    return v.toString().split('').reduce(function (a, b) { return +a + +b; });
}

var array = [56, 65, 74, 100, 99, 207, 45, 68, 86, 180, 90];

array.sort(function (a, b) {
    return weight(a) - weight(b) || a.toString().localeCompare(b);
});

console.log(array);
      

.as-console-wrapper { max-height: 100% !important; top: 0; }
      

Run codeHide result


+3


source


I think you are looking for something like this

   var arr = [56, 65, 74, 100, 99, 207, 45, 68, 86, 180, 90];
    arr.sort(function(a, b) {
      var A = ("" + a).split("").reduce(function(c, d) {
        return Number(c) + Number(d);
      });
      var B = ("" + b).split("").reduce(function(e, f) {
        return Number(e) + Number(f);
      });
      if (A > B) {
        return A - B;
      } else if (A == B) {
        return a.toString() - b.toString();
      }
    });

    console.log(arr);

      



Hope this helps!

+1


source


You can use a function sumDigits

to get the weight, then inside your sort, call that function and compare.

var arr = [56, 65, 74, 100, 99, 207, 45, 68, 86, 180, 90];

arr.sort(function(a, b) {
  var A = sumDigits(a);
  var B = sumDigits(b);

  if (A > B) {
    return 1;
  } else {
    return -1;
  }
});

console.log(arr);


function sumDigits(number) {
  var str = number.toString();
  var sum = 0;

  for (var i = 0; i < str.length; i++) {
    sum += parseInt(str.charAt(i), 10);
  }

  return sum;
}
      

Run codeHide result


0


source







All Articles