i'm trying to sort a string in java, below is my code:
public class SortingString {
public static void Sortstring(){
String str="String";
char [] charStr = str.toCharArray();
for (char value : charStr) {
System.out.println("Value = " + value);
}
Arrays.sort(charStr);
System.out.println("The sorted array is:");
for (char value : charStr) {
System.out.println("Value = " + value);
}
}
public static void main(String [] args){
Sortstring();
}
}
So when I give an input str="Java", it gives me:
Value = J
Value = a
Value = v
Value = a
The sorted array is:
Value = J
Value = a
Value = a
Value = v
And when I give input str="String", it gives me:
Value = S
Value = t
Value = r
Value = i
Value = n
Value = g
The sorted array is:
Value = S
Value = g
Value = i
Value = n
Value = r
Value = t
I don't understand why it isn't sorting all the characters. It's not sorting the first character but it is working well for rest of the characters in the String. What am I missing?