import java.util.Arrays;
import java.util.Comparator;
public class StringSortMyself {
public String[] solution(String[] strings, int n) {
Arrays.sort(strings);
Arrays.sort(strings, new Comparator<String>() {
public int compare(String o1, String o2) {
if (o1.charAt(n) < o2.charAt(n)) {
return -1;
} else if (o1.charAt(n) > o2.charAt(n)) {
return 1;
} else {
return 0;
}
}
});
return strings;
}
public static void main(String[] args) {
StringSortMyself s = new StringSortMyself();
String arr1[] = { "sun", "bed", "car" };
String arr2[] = { "abce", "abcd", "cdx" };
String arr3[] = { "cfd", "cbs", "fcc", "ads" };
String arr4[] = { "axa", "aaa", "aaas" };
System.out.println(s.solution(arr1, 1));
System.out.println(s.solution(arr2, 2));
System.out.println(s.solution(arr3, 2));
System.out.println(s.solution(arr4, 0));
}
}
Arrays.sort(strings, new Comparator<String>() { 바디 }); - 익명클래스 활용
int compare함수 작성시 return 0; 처리 필수