Ordinary Cardinals

Ordinary Cardinals

Sorting can be performed meaningfully according to arbitrary comparison criteria, as long as those criteria satisfy the mathematical requirements of a total ordering relation. To play around with this concept to grok it, let us define a wacky ordering relation of positive integers so that for any two integers, the one that contains the digit 9 more times is considered to be larger, regardless of the magnitude and other digits of these numbers.

For example, 99 > 123456789 > 1010 in this ordering. If both integers contain the digit 9 the same number of times, the comparison proceeds to the next lower digit 8, and so on down there until the first distinguishing digit has been discovered. If both integers contain every digit from 9 to 0 pairwise the same number of times, the ordinary integer order comparison determines their mutual ordering.

Your goal is to sort given sequence of integers according to this rule in ascending order. Let's see an additional example for [111, 19, 919, 1199, 911, 999] input.

example

Input: Array of integers (number).

Output: Array of integers (number).

Examples:

assert.deepStrictEqual(
    sortDigitCount([99, 123456789, 10000000000]),
    [10000000000, 123456789, 99],
);
assert.deepStrictEqual(
    sortDigitCount([9876, 19, 4321, 99, 73, 241, 111111, 563, 33]),
    [111111, 33, 241, 4321, 563, 73, 19, 9876, 99],
);
assert.deepStrictEqual(
    sortDigitCount([111, 19, 919, 1199, 911, 999]),
    [111, 19, 911, 919, 1199, 999],
);
assert.deepStrictEqual(
    sortDigitCount([1234, 4321, 3214, 2413]),
    [1234, 2413, 3214, 4321],
);

The mission was taken from Python CCPS 109. It is taught for Ryerson Chang School of Continuing Education by Ilkka Kokkarinen