题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Comparator; public class Main{ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); // 收集英文字母 ArrayList<Character> alphabet = new ArrayList<>(); for (char c : str.toCharArray()) { if (Character.isLetter(c)) { alphabet.add(c); } } // 排序英文字母 alphabet.sort(new Comparator<Character>() { @Override public int compare(Character o1, Character o2) { return Character.toLowerCase(o1) - Character.toLowerCase(o2); } }); // 将非英文字母添加到对应位置 for (int i = 0; i < str.length(); i++) { if(!Character.isLetter(str.charAt(i))){ alphabet.add(i, str.charAt(i)); } } // 输出 for (Character character : alphabet) { System.out.print(character); } } }