Comparable vs Comparator
public static int compare(int x, int y){ return (x>y) ? -1 : ((x==y)? 0 : 1); }//오름차 class A implements Comparable <A> { int x; A(int x){ this.x = x; } public int compareTo(A other){ return Integer.compare(this.x, other.x); } }//오름차 class A implements Comparable<A>{ int x; public int compareTo(A other){ //양수일 경우에만 자리 바꿈 return this.x - other.x; } } //내림차 class A implements Comparable<A>{ int x; public int compareTo(A other){ return other.x - this.x; } }
Arrays.sort(numStr, new Comparator<String>() { @Override public int compare(String a, String b) { //여기서 compareTo는 String이 가진 기본 메서드 return (a+b).compareTo(b+a); } });
Arrays.sort(numStr, (a, b) -> (b+a).compareTo(a + b));
Last updated