I wrote a Stalin sort that feels like a mess in Java

I wrote a Stalin sort that feels like a mess in Java

The recently popular [Stalin Sort](https://qiita.com/tags/ Stalin Sort), The published java implementation uses a for loop that is not an extension, I thought it was a prehistoric thing because I couldn't hand over the Comparator, so I started writing it myself. I got a bad ride on the way and thought about trying to bind not only for but also if, but it turned out to be a strange code.

For the time being, it also serves as a memorial service

What was made

	public static <T extends Comparable<? super T>> List<T> stalinSort(List<T> origin) {
		return stalinSort(origin, Comparator.naturalOrder());
	}

	public static <T> List<T> stalinSort(List<T> origin, Comparator<? super T> comparator) {
		return origin.stream().reduce(new LinkedList<T>(),
				(prev, current) -> Comparator.nullsFirst(comparator).compare(prev.peekLast(), current) <= 0
						&& prev.add(current) ? prev : prev,
				(a, b) -> {
					throw new UnsupportedOperationException();
				});
	}


Maybe it works.

Recommended Posts

I wrote a Stalin sort that feels like a mess in Java
I wrote a primality test program in Java
I wrote a prime factorization program in Java
I wrote a C parser (like) using PEG in Ruby
I created a PDF in Java.
I wrote Goldbach's theorem in java
Implement something like a stack in Java
I wrote a Lambda function in Java and deployed it with SAM
I made a primality test program in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I made a rock-paper-scissors game in Java (CLI)
A bat file that uses Java in windows
[java] sort in list
I made a simple calculation problem game in Java
I want to do something like "cls" in Java
I wrote a test code (Junit & mockit) for the code that calls the AWS API (Java)
Write a class that can be ordered in Java
I wrote about Java downcast in an easy-to-understand manner
I tried to make a login function in Java
I made a library that works like a Safari tab !!
I tried to implement Stalin sort with Java Collector
What I learned when building a server in Java
I made a Wrapper that calls KNP from Java
A library that realizes multi-line strings in Java multiline-string
I made a JAVA framework "numatrix" that easily generates unique numbers in a distributed environment & multithreading.
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
I'm like writing a process that keeps increasing itself (list) in a list loop in Java (at work)
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
I tried to make a client of RESAS-API in Java
I wrote a code to convert numbers to romaji in TDD
[Neta] Sleep Sort in Java
I made roulette in Java.
Find a subset in Java
I tried metaprogramming in Java
I created a Selenium sample app that supports multiple browsers (Chrome, IE, Firefox) that runs in Java.
Sort List in descending order in Java and generate a new List non-destructively
Create a named Skip List like redis sorted set in Java
I can't create a Java class with a specific name in IntelliJ
I tried to write code like a type declaration in Ruby
How to implement a job that uses Java API in JobScheduler
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
[Ruby 3.0] A memo that I added a type definition to a library I wrote
I wrote a route search program in TDD and refactored it
[Note] What I learned in half a year from inexperienced (Java) (3)
A story that I finally understood Java for statement as a non-engineer
I sent an email in Java
Java version notation that changes in Java 10
3 Implement a simple interpreter in Java
I made a shopify app @java
Sort a List of Java objects
A simple sample callback in Java
I made an annotation in Java.
I tried using JWT in Java
Get stuck in a Java primer
Implementation of like function in Java
How to test a private method in Java and partially mock that method