题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<String> list = new ArrayList<>();
while (n>0){
String s = sc.next();
list.add(s);
n--;
}
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
char[] s1 = o1.toCharArray();
char[] s2 = o2.toCharArray();
int l=0; //字符串长度记录
while (l < s1.length&&l<s2.length){
if (s1[l] > s2[l]){
return 1;
}else if (s1[l] < s2[l]){
return -1;
}else{
l++;
}
}
if (l == s1.length){
return -1;
}
if (l == s2.length){
return 1;
}
return 0;
}
});
for (String s:list
) {
System.out.println(s);
}
}
}