Sorting using java comparator

--Comparator can be used for convenient sorting.

Code (ascending sort)


import java.util.*;

class Solution {

    public static void main(String[] args) {
        sort1();
        sort2();
    }

    public static void sort1 () {
        List<Integer> list1 = Arrays.asList(3,2,8,4);
        System.out.println("Before sort: "+list1);
        Comparator<Integer> comparator = new Comparator<Integer>() {
            public int compare(Integer int1, Integer int2) {
                return (int1.compareTo(int2));
            }
        };
        Collections.sort(list1, comparator);
        System.out.println("comparator(Instantiate),After sort: "+list1);
    }

    public static void sort2(){
        List<Integer> list1 = Arrays.asList(3,2,8,4);
        System.out.println("Before sort: "+list1);
        Collections.sort(list1, (a, b) -> Integer.compare(a, b));
        System.out.println("Lambda-style comparator,After sort: "+list1);
    }
}

Output result

Before sort: [3, 2, 8, 4]
comparator(Instantiate),After sort: [2, 3, 4, 8]
Before sort: [3, 2, 8, 4]
Lambda-style comparator,After sort: [2, 3, 4, 8]

Recommended Posts

Sorting using java comparator
Scraping practice using Java ②
Scraping practice using Java ①
Try using RocksDB in Java
Try scraping using java [Notes]
[Java] Comparator of Collection class
Using Mapper with Java (Spring)
I tried using Java REPL
Using Docker from Java Gradle
Make a rhombus using Java
Bubble sort using ArrayList (JAVA)
Java lambda expressions learned with Comparator
Using Java on OSX 10.15 (Catalina) β
Export issues using JIRA's Java API
[Java8] Proper use of Comparable and Comparator in terms of employee sorting
Encrypt using RSA cryptography in Java
Upload a file using Java HttpURLConnection
Java comparison using the compareTo () method
Sorting a list with an int array as an element (Java) (Comparator)
Try using Redis with Java (jar)
[Java] Boilerplate code elimination using Lombok
Handling of time zones using Java
Create a Java project using Eclipse
Java
Unexpected exception when using Java DateTimeFormatter
I tried using Java8 Stream API
Using Java with AWS Lambda-Eclipse Preparation
[Java] Boilerplate code elimination using Lombok 2
[Java] Try to implement using generics
HTTPS connection using tls1.2 in Java 6
I tried using JWT in Java
Html5 development with Java using TeaVM
Formatting an enum using formatter-maven-plugin (Java)
Try using IBM Java method tracing
Java
Deleting files using recursive processing [Java]
Summary of object-oriented programming using Java
Sample code using Minio from Java
Using proxy service with Java crawling
I tried using Java memo LocalDate
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
I tried using GoogleHttpClient of Java
How to sort a List using Comparator
Try using Java framework Nablarch [Web application]
I tried using Elasticsearch API in Java
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
Whether to use Java Comparable or Comparator
Data processing using stream API from Java 8
Memory measurement for Java apps using jstat
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
About Spring Dependency Injection using Java, Kotlin
Newcomer training using the Web-Basic programming using Java-
Try using the Stream API in Java
Using JupyterLab + Java with WSL on Windows 10
Map without using an array in java
Sort by multiple conditions using Java Stream
[Java] How to calculate age using LocalDate
Notes on operators using Java ~ String type ~
[Java] Send an email using Amazon SES
Study Java Try using Scanner or Map