题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int n = sc.nextInt(); sc.nextLine(); String[] ss = new String[n]; for(int i = 0; i < n; ++i){ ss[i] = sc.nextLine(); } Arrays.sort(ss, new Comparator<String>(){ @Override public int compare(String s1, String s2){ char[] c1 = s1.toCharArray(); char[] c2 = s2.toCharArray(); if(c1.length == 0){ return -1; } if(c2.length == 0){ return 1; } if(c1[0] < c2[0]){ return -1; }else if(c1[0] > c2[0]){ return 1; }else{ int index2 = 1; int index3 = 1; while(index2 <= c1.length - 1 && index3 <= c2.length -1){ if(c1[index2] < c2[index3]){ return -1; }else if(c1[index2] > c2[index3]){ return 1; }else{ index2++; index3++; } } if(index2 <= c1.length - 1){ return 1; } if(index3 <= c2.length - 1){ return -1; } return -1; } } }); for(String s : ss){ System.out.println(s); } } } }