How can I sort a string "13,5,8,4,2,1,9"
in ascending order, to get 1,2,4,5,8,9,13
?
The sort() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
The main logic is to toCharArray() method of the String class over the input string to create a character array for the input string. Now use Arrays. sort(char c[]) method to sort character array. Use the String class constructor to create a sorted string from a char array.
If any of those steps causes you difficulties, please be more specific.
String.split()
.Integer.valueOf()
.StringBuilder
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With