Skip to content

Instantly share code, notes, and snippets.

@kkonyshev
Created August 14, 2017 11:32
Show Gist options
  • Save kkonyshev/1a32e2624e89dc8c195b50eaf8aa95ce to your computer and use it in GitHub Desktop.
Save kkonyshev/1a32e2624e89dc8c195b50eaf8aa95ce to your computer and use it in GitHub Desktop.
multisort
package your.package;
import org.junit.Test;
import java.util.AbstractMap;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
/**
* Created by Konstantin Konyshev on 14/08/2017.
*/
public class Cross {
@Test
public void test() {
String s = "бор сапог сарай арбуз болт бокс биржа бар";
Map<String, List<String>> result =
Stream.of(s.split(" "))
.sorted(Comparator.comparing((String e) -> e.substring(0, 1)).thenComparing(e -> e.length()).reversed().thenComparing(e -> e.toString()))
.map(word -> new AbstractMap.SimpleEntry<>(word.substring(0, 1), word))
.collect(java.util.stream.Collectors.groupingBy(
AbstractMap.SimpleEntry::getKey,
java.util.stream.Collectors.mapping(AbstractMap.SimpleEntry::getValue, java.util.stream.Collectors.toList()))
)
;
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment