프로그래머스
1. Python
import functools
def comparator(a,b):
t1 = a+b
t2 = b+a
return (int(t1) > int(t2)) - (int(t1) < int(t2))
def solution(numbers):
n = [str(x) for x in numbers]
n = sorted(n, key=functools.cmp_to_key(comparator),reverse=True)
answer = str(int(''.join(n)))
return answer
2. C++
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool compare(string a, string b){
return a + b > b + a;
}
string solution(vector<int> numbers) {
string answer = "";
vector<string> temp;
for (int num : numbers)
temp.push_back(to_string(num));
sort(temp.begin(), temp.end(), compare);
if (temp.at(0) == "0") return "0";
for (auto num : temp)
answer += num;
return answer;
}
3. JavaScript
function solution(numbers) {
var answer = numbers.map(c => c + '').
sort((a,b) => (b + a) - (a + b)).join('');
return answer[0] === '0'? '0' : answer;
}