题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int count = in.nextInt(); List<String> list = new ArrayList<>(count); for (int i = 1; i <= count; i ++) { list.add(in.next()); } list.sort((o1, o2) -> { char[] chars = o1.toCharArray(); char[] chars1 = o2.toCharArray(); for (int i = 0; i < Math.min(chars.length, chars1.length); i ++) { if (chars[i] == chars1[i]) { continue; } return chars[i] - chars1[i]; } if (chars.length != chars1.length) { return chars.length - chars1.length; } return 0; }); for (String s : list) { System.out.println(s); } } } }