Computer Science, asked by vivanshekar51, 1 year ago

write a function named int sort_digit(int) in Java to pass an integer number as argument that might contain any number of digits and sort it each individual digits of the number in ascending order and return the same without implementing erase example sample input 4987 sample output 4 7 8 9

Answers

Answered by timothymathew
2

public int orderDigits(int a) {

String str = a + "";

char[] charArray = str.toCharArray();

Arrays.sort(charArray);

if (a>0)

{

String Sorted = new String(charArray);

return Integer.parseInt(Sorted);

}

else if (a<0)

{

int size = 1;

while(a!=0){

size++;

a /= 10;

}

String tempStr = new String(charArray);

String withoutMinus = tempStr.substring(1);

String withoutZero = withoutMinus.substring(0, withoutMinus.length());

StringBuffer sb = new StringBuffer(withoutZero);

sb.reverse();

String revStr = sb + "";

return -(Integer.parseInt(revStr));

}

else{

return 0;

}

}

Similar questions