[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests

background

Recently, I've been declining in the WEB coding test for job change activities. I'm practicing at paiza.io because I think I have to get used to it. If you are too busy to solve it, you will see the source at a later date and mass-produce the nauseating fucking code.

This time, I will summarize the method of sorting after storing in List .

Operating environment

java.util.Comparator;Imagine you are importing.



# Alphabetical order

```java
List<String> list = Arrays.asList("ABC", "DEFGH", "IJKL", "abc", "defgh", "ijkl");

//Alphabetical ascending order (case insensitive)
//Output result: ABC,abc,DEFGH,defgh,IJKL,ijkl
list.sort(String.CASE_INSENSITIVE_ORDER);

//Descending alphabetical order (case insensitive)
//Output result: IJKL,ijkl,DEFGH,defgh,ABC,abc
list.sort(String.CASE_INSENSITIVE_ORDER.reversed());

//Alphabetical ascending order (case sensitive)
//Output result: ABC,DEFGH,IJKL,abc,defgh,ijkl
list.sort(Comparator.naturalOrder());

//Descending alphabetical order (case sensitive)
//Output result: ijkl,defgh,abc,IJKL,DEFGH,ABC
list.sort(Comparator.reverseOrder());

String length order

List<String> list = Arrays.asList("ABC", "DEFGH", "IJKL", "abc", "defgh", "ijkl");

//Ascending order of string length
//Output result: ABC,abc,IJKL,ijkl,DEFGH,defgh
list.sort(Comparator.comparingInt(String::length));

//String length descending
//Output result: DEFGH,defgh,IJKL,ijkl,ABC,abc
list.sort(Comparator.comparingInt(String::length).reversed());

bonus

Character string length → Sort alphabetically

list.sort(Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()));

How to sort Integer.

List<Integer> list = Arrays.asList(1, 3, 2);

//ascending order
Collections.sort(list);

//descending order
Collections.reverse(list);

At the end

It can be realized with Lambda, but I personally feel that Comparator is more readable, so I omitted it. I want to come out quickly without investigating this much.

Coding tests often require this sort of sorting, so it's a good idea to remember! This one is more readable! Please let us know if you have any opinions or mistakes.

Recommended Posts

[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
Syntax and exception occurrence conditions that can be used when comparing with null in Java
Technology excerpt that can be used for creating EC sites in Java training
Write a class that can be ordered in Java
Scala String can be used other than java.lang.String method
Convenient shortcut keys that can be used in Eclipse
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
Problems that can easily be mistaken for Java and JavaScript
Java (super beginner edition) that can be understood in 180 seconds
Reference memo / In-memory LDAP server that can be embedded in Java
Static analysis tool that can be used on GitHub [Java version]
Note that system properties including JAXBContext cannot be used in Java11
SwiftUI View that can be used in combination with other frameworks
Java file input / output processing that can be used through historical background
[Rails] "pry-rails" that can be used when saving with the create method
[Android Studio] Description that can be continuously input in SQLite Database [Java]
Performance analysis and failure diagnostic tools that can be used with OpenJDK
Member description order in Java coding convention
Create KeyStore and sign apk in processing (android mode) (java can be used if some changes are made)
[Java] Difference between equals and == in a character string that is a reference type
Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
Why Java String typeclass comparison (==) cannot be used
The story that the port can no longer be used in the Spring boot sample program
Compiled kotlin with cli with docker and created an environment that can be executed with java
The story that the variable initialization method called in the Java constructor should not be overridden