Java
The idea is to maximize the number obtained from digits if the original number is negative and to minimize the number if positive.\nI.e. 1234 < 4321 but -1234 >
The idea is to maximize the number obtained from digits if the original number is negative and to minimize the number if positive.\nI.e. **1234 < 4321 but -1234 > -4321**.\n```\nclass Solution\n{\n    public long smallestNumber(long num)\n    {\n        boolean neg = false;\n        if(num < 0) //Remove negative sign\n        {\n            neg = true;\n            num = -num;\n        }\n        char[] dig = String.valueOf(num).toCharArray();\n        Arrays.sort(dig); // Ascending sort\n        if(!neg) // For positive number, swap the first non zero with 0th index if 0 is present\n        {\n            int i = 0;\n            for(i = 0; i < dig.length; i++)//Finding the first non zero\n                if(dig[i] != \'0\')\n                    break;\n            if(i <= dig.length-1)//Swapping\n            {\n                char temp = dig[i];\n                dig[i] = dig[0];\n                dig[0] = temp;\n            }\n        }\n        long ret = 0L;\n        if(neg)//Requires descending sort, hence reverse traversal\n        {\n            StringBuilder sb = new StringBuilder();\n            for(int i = dig.length-1; i >= 0; i--)\n                sb.append(dig[i]);\n            ret = Long.parseLong(sb.toString());\n            ret = -ret;\n        }\n        else\n            for(char c : dig)\n                ret = ret * 10 + (c - \'0\');\n        return ret;\n    }\n}\n```
['Java']