I have a function that sorts an array of integers by their digits sum, in case of equal digit sum than it sort by the numbers values. This is the function:
function s(g){
    var r=0;
    while(g)r+=g%10,g/=10;
    return r;
}
function digitalSumSort(a) {
    a.sort(function(x,y){
        return s(x)!=s(y)?s(x)-s(y):x-y;
    });
    return a;
}
It sometimes works ok, but it failed at this test data:
Input: [100, 22, 4, 11, 31, 103]
Output: [100, 11, 31, 4, 22, 103]
Expected Output: [100, 11, 4, 22, 31, 103]
I couldn't figure out why it does that, and how to fix it?
Note: it is important that the code contains as least as possible characters!
Edit:
This question has already been answered but I recently made the same mistake and thought of this. Is there a way to make a var act as an integer (instead of a double) when given a numeric value. I learned the trick with using floor, but sometimes I just want integer operations instead of double (with some systems they are faster, and I may have other reasons).
 
    