See the original problem on HackerRank.
MegaBytus is a young and promising wizard who loves programming and computers. The master GigaBytus gave him a task: crafting a spell to rearrange the digits of a number such that the number obtained is the smallest possible.
MegaBytus needs your help to find a way to verify that his spell works. Then you have to write a program that, given an integral number, will find the smallest number obtained by rearranging its digits.
Can you help him?
Input Format
The number N
Constraint
N has at least 6 digits and at most 19.
Output Format
The new number, on a single line
Solutions
The resulting number is produced by putting the smallest nonzero number first and then all the others, in increasing order (including duplicates).
Here is a a C++ solution based on frequency table:


A Javascript alternative by Simone Busoli:


Alternatives involve sorting with special comparison functions, left as exercises.