List an Array of Strings in alphabetical order

前端 未结 11 1391
独厮守ぢ
独厮守ぢ 2020-12-28 14:49

I have a program which has the user inputs a list of names. I have a switch case going to a function which I would like to have the names print off in alphabetical order.

11条回答
  •  有刺的猬
    2020-12-28 15:07

     public static String[] textSort(String[] words) {
        for (int i = 0; i < words.length; i++) {
            for (int j = i + 1; j < words.length; j++) {
                if (words[i].compareTo(words[j]) > 0) {
                    String temp = words[i];
                    words[i] = words[j];
                    words[j] = temp;
                }
            }
        }
    
        return words;
    }
    

提交回复
热议问题