github | https://github.com/JasonZhangCauc/JZOffer |
---|
import java.util.Arrays;
import java.util.Comparator;
public class Test32 {
public static class MyComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return Integer.parseInt(o1 + o2) - Integer.parseInt(o2 + o1);
// return (o1+o2).compareTo(o2+o1);
}
}
public static String PrintMinNumber(int[] numbers) {
String[] arrays = new String[numbers.length];
for (int p = 0; p < numbers.length; p++) {
arrays[p] = numbers[p] + "";
}
String string = "";
Arrays.sort(arrays, new MyComparator());
for (int k = 0; k < arrays.length; k++) {
string += arrays[k];
}
return string;
}
public static void main(String[] args) {
int array[] = { 11, 56, 89, 15 };
System.out.println(PrintMinNumber(array));
int array2[] = { 3, 32, 321 };
System.out.println(PrintMinNumber(array2));
}
}