Sort strings functionally with java

What I want to do is when I have a list of strings " insert "," create "," drop "`` `, I want to sort them in my favorite order instead of the natural order. Specifically, I want to sort in the order " drop "," create "," insert "` ``.

In this case, since the elements of the set are limited, in SQL it is sufficient to sort by `` `case``` with the characteristic function. I want to do the same thing with java.

environment

java 1.8

code

Have a characteristic function in Map

It has a characteristic function equivalent in Map. key is an element of the set and value is an integer in the sort order.

Map<String, Integer> map = new HashMap<>();
map.put("drop", 1);
map.put("create", 2);
map.put("insert", 3);

List<String> list = Arrays.asList("insert", "create", "drop");
list.stream()
	.sorted((s1, s2) -> map.get(s1).compareTo(map.get(s2)))
	.forEach(System.out::println);

Output result.

drop
create
insert

Sort file names in any order

The situation where this was needed was that I wanted to sort the files in a particular order. For example, suppose you have a group of files like this.

0001_create.sql
0001_drop.sql
0001_insert.sql
0002_create.sql
0002_drop.sql
0002_insert.sql

I wanted to get the file list in the order of prefix + suffix in the above-mentioned [drop-> create-> insert].

So the source code.

Map<String, Integer> map = new HashMap<>();
map.put("drop.sql", 1);
map.put("create.sql", 2);
map.put("insert.sql", 3);

Files.walk(Paths.get("files"))
	.filter(Files::isRegularFile)
	.sorted((p1, p2) -> {
		String[] sp1 = p1.getFileName().toString().split("_");
		String[] sp2 = p2.getFileName().toString().split("_");

		String pp1 = sp1[0] + map.get(sp1[1]);
		String pp2 = sp2[0] + map.get(sp2[1]);
		return pp1.compareTo(pp2);})
	.forEach(System.out::println);

Output result.

files\0001_drop.sql
files\0001_create.sql
files\0001_insert.sql
files\0002_drop.sql
files\0002_create.sql
files\0002_insert.sql

That doesn't change what you do. Separate the file names with a separator and convert the suffix part to an integer in the sort order with a characteristic function map. If you attach them in the order of prefix, you will get the strings `" 00013 "," 00012 "," 00011 "```, so all you have to do is `compareTo```.

Recommended Posts

Sort strings functionally with java
java bubble sort
Manipulating Java strings
java selection sort
[Java] Precautions when comparing character strings with character strings
java insertion sort
Calculate the similarity score of strings with JAVA
[java] sort in list
Install java with Homebrew
Change seats with java
Install Java with Ansible
Comfortable download with JAVA
Java Japanese (Kanji) Sort
Switch java with direnv
Download Java with Ansible
Let's scrape with Java! !!
Build Java with Wercker
Endian conversion with JAVA
[Java] Sort ArrayList with elements of your own class
I tried to implement Stalin sort with Java Collector
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
[Template] MySQL connection with Java
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
Java to play with Function
Enable Java EE with NetBeans 9
[Java] JavaConfig with Static InnerClass
Let's operate Excel with Java! !!
Version control Java with SDKMAN
RSA encryption / decryption with java 8
Paging PDF with Java + PDFBox.jar
Object-oriented (java) with Strike Gundam
[Java] Content acquisition with HttpCliient
Java version control with jenv
Troubleshooting with Java Flight Recorder
Streamline Java testing with Spock
Connect to DB with Java
Connect to MySQL 8 with Java
Error when playing with java
Using Mapper with Java (Spring)
Java study memo 2 with Progate
Getting Started with Java Basics
Seasonal display with Java switch
Use SpatiaLite with Java / JDBC
Study Java with Progate Note 1
Compare Java 8 Optional with Swift
Java List Group, Sort, etc.
HTML parsing with JAVA (scraping)
Run Java VM with WebAssembly
Bubble sort using ArrayList (JAVA)
Screen transition with swing, java
Java unit tests with Mockito
[Java 8] Duplicate deletion (& duplicate check) with Stream