My program takes a string as input from the user. This method in particular gives back every unique character in the string and the amount of times it has been repeated. The output is sorted in ASCII order (lowest ASCII value first). Is there a way to sort this list in accordance with the number of times each character appears (i.e. most common character first, least common character last)? Moreover, is there a way, in the event that two characters appear the same amount of times, that the first character on the ASCII table is listed first? This is the code I'm trying to work off of.
public static void alphabeticalSort(String input)
{
int[] ascii = new int[256];
for (int i = 0; i < input.length(); i++) {
char current = input.charAt(i);
ascii[(int)current]++;
}
System.out.println(IntStream.range(0, ascii.length).filter(x -> ascii[x] > 0).mapToObj(x -> String.format("%s: freq %s", (char) x, ascii[x])).collect(Collectors.joining("\n")));
}