Skip to content

Instantly share code, notes, and snippets.

@svpino
Created May 12, 2015 23:49
Show Gist options
  • Save svpino/03892b9c49cad69e0b21 to your computer and use it in GitHub Desktop.
Save svpino/03892b9c49cad69e0b21 to your computer and use it in GitHub Desktop.
Problem 4 in "Five programming problems every Software Engineer should be able to solve in less than 1 hour"
import java.util.Arrays;
import java.util.Comparator;
// Solution to Problem 4 posted in "Five programming problems every Software Engineer should be able to solve in less than 1 hour"
// Original post: https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
public class Main {
private static Integer[] VALUES = { 5, 2, 1, 9, 50, 56 };
public static void main(String[] args) {
Arrays.sort(VALUES, new Comparator() {
@Override
public int compare(Integer lhs, Integer rhs) {
String v1 = lhs.toString();
String v2 = rhs.toString();
return (v1 + v2).compareTo(v2 + v1) * -1;
}
});
String result = "";
for (Integer integer : VALUES) {
result += integer.toString();
}
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment