[JAVA] 100% FAST, EXPLAINED WITH COMMENTS
\'\'\'\nclass Solution {\n    public long smallestNumber(long num) {\n        \n        // convert the number into a string\n        \n        String str;\n    
\'\'\'\nclass Solution {\n    public long smallestNumber(long num) {\n        \n        // convert the number into a string\n        \n        String str;\n        \n        if(num < 0)\n        {\n            long nums = num*(-1);\n            str = String.valueOf(nums);\n        }\n        else\n        {\n            str = String.valueOf(num);\n        }\n        \n        char arr[] = str.toCharArray();\n        \n        long ans;\n        \n        if(num < 0) //negative\n        {\n            //sort the character array in descending\n            // with 0 \n            \n            Arrays.sort(arr);   // from this we will get the ascending order, but we need the opposite of it\n            reverse(arr);\n            \n            String res = String.valueOf(arr);   //converting the charcter array back to string\n            ans = Long.parseLong(res); //converting string to the long value \n            ans = -ans;\n        }\n        else\n        {\n            //if the number is positive\n            Arrays.sort(arr);\n            \n            //Here, we will have 0 in the front so, we have to swap it with the first non-zero element in the array    \n            int i=0;\n            \n            for(i = 0;i<arr.length;i++)\n            {\n                if(arr[i] != \'0\')\n                {\n                    //we get the last i value where the 0 starts\n                    break;\n                }\n            }\n            \n            if(i <= (arr.length-1)) //if there is an 0 in the array\n            {\n                char temp = arr[i];\n                arr[i] = arr[0];\n                arr[0] = temp;\n            }\n            \n            String res = String.valueOf(arr);   //converting the charcter array back to string\n            ans = Long.parseLong(res); //converting string to the long value \n            \n        }\n        \n        return ans;\n    }\n    \n    public void reverse(char[] arr)\n    {\n        // we will traverse half of the array and will put all the element of the first half, in the last one.\n        // in simle words 1st element as last, 2nd element as 2nd last ....\n        \n        int len = arr.length;\n        \n        for(int i=0;i<arr.length/2;i++)\n        {\n            char temp = arr[i];\n            arr[i] = arr[len-i-1];\n            arr[len-i-1] = temp;\n        }\n    }\n}\n\'\'\'
['Java']