Created
August 14, 2015 01:16
-
-
Save wbars/e4a3dbfb91a53f57b10d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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