* 위와 같은 클래스를 만들 필요 없이 아래의 코드로 간단하게 정렬 순서를 정의할 수 있다
```java
Collections.sort(list, (o1, o2) -> {
String a = String.valueOf(o1) + String.valueOf(o2);
String b = String.valueOf(o2) + String.valueOf(o1);
return -(Integer.parseInt(a) - Integer.parseInt(b));
});
🆘 이해가 어려운 내용
❌ 해결하지 못한 이유
숫자를 문자열로 통째로 합친 뒤 그 크기를 비교하여 순서가 정렬되도록 할 생각을 못했다
✅ 본인 풀이
원래 생각 했던 풀이(실패)
```java
public Number(int n) {
this.input = n;
if (n < 10) {
/* 9 */
this.first = n;
this.second = this.first;
this.third = this.first;
this.forth = this.first;
}
if (n >= 10 && n < 100) {
/* 99 */
this.first = n / 10;
this.second = n % 10;
this.third = this.first;
// this.forth = this.second;
}
if (n >= 100 && n < 1000) {
/* 999 */
this.first = n / 100;
this.second = n % 100 / 10;
this.third = n % 100 % 10;
this.forth = this.first;
}
if (n >= 1000) {
/* 1111 */
this.first = n / 1000;
this.second = n % 1000 / 100;
this.third = n % 1000 % 100 / 10;
this.forth = n % 1000 % 100 % 10;
}
}
@Override
public int compareTo(Number o) {
if (this.first == o.first) {
if (this.second == o.second) {
if (this.third == o.third) {
return this.input - o.input;
}
return this.third - o.third;
}
return this.second - o.second;
}
return this.first - o.first;
}
public int getInput() {
return input;
}
}
```
풀이
class Solution42746 {
public String solution(int[] numbers) {
List<Number> list = new ArrayList<>();
for (int number : numbers) {
list.add(new Number(number));
}
Collections.sort(list, Comparator.reverseOrder()); /* 내림차순 */
StringBuilder sb = new StringBuilder();
list.forEach(number -> sb.append(number.getInput()));
if (sb.charAt(0) == '0') { /* {0,0,0} -> "0" ("000" -> X)*/
return "0";
}
return sb.toString();
}
static class Number implements Comparable<Number> {
private String input;
public Number(int num) {
this.input = String.valueOf(num);
}
@Override
public int compareTo(Number o) {
String a = this.input + o.input;
String b = o.input + this.input;
return Integer.parseInt(a) - Integer.parseInt(b);
}
public String getInput() {
return input;
}
}
}
개선된 풀이
class Solution42746 {
public String solution(int[] numbers) {
List<Integer> list = new ArrayList<>();
for (int number : numbers) {
list.add(number);
}
Collections.sort(list, (o1, o2) -> {
String a = String.valueOf(o1) + String.valueOf(o2);
String b = String.valueOf(o2) + String.valueOf(o1);
return -(Integer.parseInt(a) - Integer.parseInt(b));
});
StringBuilder sb = new StringBuilder();
list.forEach(integer -> sb.append(integer));
if (sb.charAt(0) == '0') { /* {0,0,0} -> "0" */
return "0";
}
return sb.toString();
}
📌 문제
⭐️ 아이디어
compareTo()
를 오버라이딩한 뒤Collections.sort()
를 해보면 어떨까🤔 고민한 내용
{978, 97}
,{979, 97}
,{232, 23}
같은 반례들을 어떻게 동시에 만족 시키지..?💪 새롭게 배운 내용
정렬 순서를 정의하기 위해
Comparable
을 구현하는 Class를 따로 만들 필요없이 람다식을 이용해 간단하게 구현할 수 있다... Collections.sort(list, Comparator.reverseOrder());
🆘 이해가 어려운 내용
❌ 해결하지 못한 이유
✅ 본인 풀이
원래 생각 했던 풀이(실패)
```java public Number(int n) { this.input = n; if (n < 10) { /* 9 */ this.first = n; this.second = this.first; this.third = this.first; this.forth = this.first; } if (n >= 10 && n < 100) { /* 99 */ this.first = n / 10; this.second = n % 10; this.third = this.first; // this.forth = this.second; } if (n >= 100 && n < 1000) { /* 999 */ this.first = n / 100; this.second = n % 100 / 10; this.third = n % 100 % 10; this.forth = this.first; } if (n >= 1000) { /* 1111 */ this.first = n / 1000; this.second = n % 1000 / 100; this.third = n % 1000 % 100 / 10; this.forth = n % 1000 % 100 % 10; } } @Override public int compareTo(Number o) { if (this.first == o.first) { if (this.second == o.second) { if (this.third == o.third) { return this.input - o.input; } return this.third - o.third; } return this.second - o.second; } return this.first - o.first; } public int getInput() { return input; } } ```풀이
개선된 풀이
}