return Arrays.stream(words.split(" ")) .sorted(Comparator.comparing(s -> Integer.valueOf(s.replaceAll("D", "")))) .reduce((a, b) -> a + " " + b).get();