С сортировкой нарушается вывод символов в консоль

Программа принимает любое предложение с клавиатуры, и считает одинаковые символы. Выводится в консоль отсортированным, от большего к меньшему.

Например : АББСС -> 1А 2Б 2С

public class CalcuChar {

    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);

        while (true) {

            System.out.print("Слово или фраза: ");
            String string = scan.nextLine();

            if (string.isEmpty()) {
                System.out.println("Вы ничего не ввели!");
                break;
            }

            int[] counters = new int [6600];

            for (char c : string.toCharArray()) {
                counters[(int) c]++;
            }

            boolean sortirovka = true;
            while (sortirovka) {
                sortirovka = false;
                for (int i = 1; i < counters.length; i++) {
                        if (counters[i] > counters[i-1]) {
                            swap(counters, i, i- 1);
                            sortirovka = true;
                        }
                }
            }


            System.out.println("Результат:");
            System.out.println("--------------------------");
            for (int i = 0; i < counters.length; i++) {
                if (counters[i] > 0)
                    System.out.println((char) i+ "   " + counters[i]);
            }

            }

        }

    static void swap(int[] array, int pos1, int pos2) {
        int tmp = array[pos1];
        array[pos1] = array[pos2];
        array[pos2] = tmp;
    }

}

Ответы (1 шт):

Автор решения: KindMax

Почитал и сделал так:

Map<Character,Integer> amountChar = new HashMap<>();

        for (char c : string.toCharArray()){
            if(amountChar.keySet().contains(c)){
                amountChar.put(c, amountChar.get(c) + 1);
            } else {
                amountChar.put(c,1);
            }
        }

А сортировку уже сделал через .entrySet().stream()

→ Ссылка