JavaBean 多字段排序

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/15618581.html

 

Project Directory

JavaBean 多字段排序

 

Maven Dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.fool</groupId>
    <artifactId>SortDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils-core</artifactId>
            <version>1.8.3</version>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils-bean-collections</artifactId>
            <version>1.8.3</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
    </dependencies>

</project>

 

SRC

SortBO.java

package org.fool.sort;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class SortBO {
    private Boolean top;
    private Integer weight;
}

 

ProductBO.java

package org.fool.sort;

import lombok.Builder;
import lombok.Data;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections4.ComparatorUtils;
import org.apache.commons.collections4.comparators.ComparableComparator;
import org.apache.commons.collections4.comparators.ComparatorChain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Data
@Builder
public class ProductBO {
    private Integer id;
    private SortBO sort;

    public static void sort(List<?> list) {
        Comparator<?> comparator1 = ComparatorUtils.booleanComparator(true);

        ComparableComparator<?> instance = ComparableComparator.INSTANCE;
        Comparator<?> comparator2 = ComparatorUtils.nullLowComparator(instance);

        List<Object> objects = new ArrayList<>();
        objects.add(new BeanComparator("sort.top", comparator1));
        objects.add(new BeanComparator("sort.weight", comparator2));
        ComparatorChain comparatorChain = new ComparatorChain(objects);

        Collections.sort(list, comparatorChain);
    }
}

 

Test.java

package org.fool.sort;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        ProductBO product1 = new ProductBO(1, new SortBO(false, 0));
        ProductBO product2 = new ProductBO(2, new SortBO(false, 1));
        ProductBO product3 = new ProductBO(3, new SortBO(true, 2));
        ProductBO product4 = new ProductBO(4, new SortBO(true, 1));

        List<ProductBO> list = Stream.of(product1, product2, product3, product4).collect(Collectors.toList());

        System.out.println("before sort: ");
        for (ProductBO record : list) {
            System.out.println(record);
        }

        ProductBO.sort(list);

        System.out.println("after sort: ");
        for (ProductBO record : list) {
            System.out.println(record);
        }
    }
}

 

Run

JavaBean 多字段排序

 

 

上一篇:java常用工具


下一篇:java导入Excel表格