Skip to content

Instantly share code, notes, and snippets.

@wbars
Created August 14, 2015 01:16
Show Gist options
  • Save wbars/e4a3dbfb91a53f57b10d to your computer and use it in GitHub Desktop.
Save wbars/e4a3dbfb91a53f57b10d to your computer and use it in GitHub Desktop.
public class Utils {
public interface SumOfDigits {
Integer run(String s);
}
public static String orderWeight(String strng) {
SumOfDigits sumOfDigits = s ->
Arrays.asList(s.split(""))
.stream()
.mapToInt(Integer::parseInt)
.sum();
return Arrays.asList(strng.split("\\s+"))
.stream()
.sorted((a, b) -> {
if (sumOfDigits.run(a).equals(sumOfDigits.run(b))) {
return a.compareTo(b);
}
return sumOfDigits.run(a).compareTo(sumOfDigits.run(b));
})
.collect(Collectors.joining(" "));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment