List中文排序&对象多属性排序

根据姓名首字母升序

引入maven依赖

<dependency>
   <groupId>org.clojars.cbilson</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.0</version>
</dependency>

贴上代码

private void orderByDoctorName(List<DoctorListResp> result) {
   Collections.sort(result, (s1, s2) -> {
        String o1 = s1.getDoctorName();
        String o2 = s2.getDoctorName();
        for (int i = 0; i < o1.length() && i < o2.length(); i++) {

            int codePoint1 = o1.charAt(i);
            int codePoint2 = o2.charAt(i);

            if (Character.isSupplementaryCodePoint(codePoint1)
                    || Character.isSupplementaryCodePoint(codePoint2)) {
                i++;
            }

            if (codePoint1 != codePoint2) {
                if (Character.isSupplementaryCodePoint(codePoint1)
                        || Character.isSupplementaryCodePoint(codePoint2)) {
                    return codePoint1 - codePoint2;
                }

                String pinyin1 = PinyinHelper.toHanyuPinyinStringArray((char) codePoint1) == null
                        ? null : PinyinHelper.toHanyuPinyinStringArray((char) codePoint1)[0];
                String pinyin2 = PinyinHelper.toHanyuPinyinStringArray((char) codePoint2) == null
                        ? null : PinyinHelper.toHanyuPinyinStringArray((char) codePoint2)[0];

                // 两个字符都是汉字
                if (pinyin1 != null && pinyin2 != null) {
                    if (!pinyin1.equals(pinyin2)) {
                        return pinyin1.compareTo(pinyin2);
                    }
                } else {
                    return codePoint1 - codePoint2;
                }
            }
        }
        return o1.length() - o2.length();
    });
}


POJO

//
package com.pica.cloud.patient.health.common.dto.wechat;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName DoctorListDto
 * @Description 我的医生列表响应对象
 * @Author Chongwen.jiang
 * @Date 2019/1/9 17:45
 * @ModifyDate 2019/1/9 17:45
 * @Version 1.0
 */
@ApiModel("医生列表响应对象")
@Data
public class DoctorListResp implements Serializable{

    @ApiModelProperty("医生id")
    private Integer doctorId;

    @ApiModelProperty("医生姓名")
    private String doctorName;

    @ApiModelProperty("医院名称(exitedHospital=2时则表示之前的医院名称)")
    private String hospitalName;

    @ApiModelProperty("医生头像")
    private String headImageUrl;

    @ApiModelProperty("医院id")
    private Long hospitalId;

    @ApiModelProperty("退出机构标识 2:已退出原有机构,尚未加入新机构")
    private Integer exitedHospital;


}

java8对多个属性排序


public static void main(String[] args) {
   List<DoctorListResp> list = new ArrayList<>();
    DoctorListResp r = new DoctorListResp();
    r.setDoctorId(1);
    r.setHospitalId(Long.valueOf(1L));

    DoctorListResp r2 = new DoctorListResp();
    r2.setDoctorId(2);
    r2.setHospitalId(Long.valueOf(2L));

    DoctorListResp r3 = new DoctorListResp();
    r3.setDoctorId(2);
    r3.setHospitalId(Long.valueOf(1L));

    DoctorListResp r4 = new DoctorListResp();
    r4.setDoctorId(3);
    r4.setHospitalId(Long.valueOf(3L));

    DoctorListResp r5 = new DoctorListResp();
    r5.setDoctorId(3);
    r5.setHospitalId(Long.valueOf(4L));

    DoctorListResp r6 = new DoctorListResp();
    r6.setDoctorId(3);
    r6.setHospitalId(Long.valueOf(7L));

    list.add(r);
    list.add(r2);
    list.add(r3);
    list.add(r4);
    list.add(r5);
    list.add(r6);

    //  1、根据doctorId升序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId))
            .collect(Collectors.toList());

    //  2、根据doctorId降序 注意两种写法:
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId).reversed())
            .collect(Collectors.toList());
    //  先以doctorId升序,结果进行doctorId降序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId, Comparator.reverseOrder()))
            .collect(Collectors.toList());

    //  3、返回 对象集合以类doctorId升序 hospitalId升序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId)
                    .thenComparing(DoctorListResp::getDoctorId))
            .collect(Collectors.toList());

    //  4、返回 对象集合以类属性一降序 属性二升序 注意两种写法:
    //  先以属性一升序,升序结果进行属性一降序,再进行属性二升序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId).reversed()
                    .thenComparing(DoctorListResp::getHospitalId))
            .collect(Collectors.toList());
    //  先以属性一降序,再进行属性二升序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId, Comparator.reverseOrder())
                    .thenComparing(DoctorListResp::getHospitalId))
            .collect(Collectors.toList());

    //  5、返回 对象集合以类属性一降序 属性二降序 注意两种写法:
    //  先以属性一升序,升序结果进行属性一降序,再进行属性二降序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId).reversed()
                    .thenComparing(DoctorListResp::getHospitalId, Comparator.reverseOrder()))
            .collect(Collectors.toList());
    //  先以属性一降序,再进行属性二降序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId,
                    Comparator.reverseOrder()).thenComparing(DoctorListResp::getHospitalId, Comparator.reverseOrder()))
            .collect(Collectors.toList());

    //  6、返回 对象集合以类属性一升序 属性二降序 注意两种写法:
    //先以属性一升序,升序结果进行属性一降序,再进行属性二升序,结果进行属性一降序属性二降序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId).reversed()
                    .thenComparing(DoctorListResp::getHospitalId).reversed())
            .collect(Collectors.toList());
    //  先以属性一升序,再进行属性二降序
    list = list.stream().sorted(
                    Comparator.comparing(DoctorListResp::getDoctorId)
                    .thenComparing(DoctorListResp::getHospitalId, Comparator.reverseOrder()))
            .collect(Collectors.toList());


    //  输出打印list集合
    System.out.println(JSON.toJSONString(list, true));


}

List集合属性复制

package com.homedo.microservice.homedo.basicdata.service.util;

import cn.hutool.core.bean.BeanUtil;
import org.apache.commons.collections.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 对象操作工具类
 * @author: jiangchongwen
 * @createTime: 2021-11-11 13:10
 */
public final class BeansUtil {
    private BeansUtil() {
    }

    /**
     * list集合拷贝
     * @param sourceList
     * @param targetClass
     * @param <T1>
     * @param <T2>
     * @return
     */
    public static <T1, T2> void listCopy(List<T1> sourceList, List<T2> targetList, Class<T2> targetClass) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return;
        }

        List<T2> collect = (List) sourceList.stream().map((source) -> {
            Object target;
            try {
                target = targetClass.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                throw new RuntimeException();
            }
            BeanUtil.copyProperties(source, target);
            return target;
        }).collect(Collectors.toList());
        targetList.addAll(collect);
    }

    /**
     * list集合拷贝
     * @param sourceList
     * @param targetClass
     * @param <T1>
     * @param <T2>
     * @return
     */
    public static <T1, T2> List<T2> listCopy(List<T1> sourceList, Class<T2> targetClass) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return Collections.emptyList();
        }

        return (List) sourceList.stream().map((source) -> {
            Object target;
            try {
                target = targetClass.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                throw new RuntimeException();
            }
            BeanUtil.copyProperties(source, target);
            return target;
        }).collect(Collectors.toList());

    }

    /**
     * map集合拷贝
     * @param sourceMap
     * @param targetMap
     * @param <T1>
     * @param <T2>
     */
    public static <T1, T2, T3> void mapCopy(Map<T1, T2> sourceMap, Map<T1, T3> targetMap) {
        BeanUtil.copyProperties(sourceMap, targetMap);
    }


}



版权声明:本文为weixin_41205148原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。