- StringBuilder and Comparator, 简单地忽略大小写排个序即可
import java.util.*; public class Main { public static String sort(String str) { // 先将英文字母收集起来 List<Character> letters = new ArrayList<>(); for (char ch : str.toCharArray()) { if (Character.isLetter(ch)) { letters.add(ch); } } // 将英文字母先排序好 letters.sort(new Comparator<Character>() { public int compare(Character o1, Character o2) { return Character.toLowerCase(o1) - Character.toLowerCase(o2); } }); // 若是非英文字母则直接添加 StringBuilder result = new StringBuilder(); for (int i = 0, j = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i))) { result.append(letters.get(j++)); } else { result.append(str.charAt(i)); } } return result.toString(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String str = in.nextLine(); String res = sort(str); System.out.println(res); } } }