java string sorting alphabetically code example
Example 1: java sort string characters alphabetically
import java.util.Arrays;
public class CharactersAlphabetically
{
public static void main(String[] args)
{
String strInput = "flowerbrackets";
char[] ch = strInput.toCharArray();
Arrays.sort(ch);
String strSorted = String.valueOf(ch);
System.out.println("sort string characters alphabetically: " + strSorted);
}
}
Example 2: java compare strings alphabetically
String a = "HYRE";
String b = "AGNYG";
int compare = a.compareTo(b);
if (compare < 0) {
}
else if (compare > 0) {
}
else {
}