Java实现app版本排序和版本比较

一、app版本排序

1、从数据库查询app数据

CREATE TABLE `app` (
  `id` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` datetime DEFAULT NULL COMMENT '创建时间',
  `modified_at` datetime DEFAULT NULL COMMENT '更新时间',
  `created_by` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '创建人',
  `modified_by` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '更新人',
  `version` int DEFAULT NULL COMMENT '版本(乐观锁)',
  `is_delete` int DEFAULT NULL COMMENT '逻辑删除',
  `tenant_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '租户编号',
  `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '名称',
  `edition` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '版本',
  `remark` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '备注',
  `uri` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '附件路径',
  `type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '类型',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='App';

插入几条数据


INSERT INTO `app`(`id`, `created_at`, `modified_at`, `created_by`, `modified_by`, `version`, `is_delete`, `tenant_code`, `name`, `edition`, `remark`, `uri`, `type`) VALUES ('4147ebd79f92bca319259441590f7c59', '2020-12-02 09:37:33', '2020-12-09 15:26:13', '05406b5219a34e04ab32e666f297c37a', '5f8c92f4841235a48e498520cea74a64', 3, 0, NULL, '爷4.1', '4.1', '修改', 'group1/M035/wKgB4V_G7-WAaeB_AfwnGpGeFnk872.apk', 'laoye');
INSERT INTO `app`(`id`, `created_at`, `modified_at`, `created_by`, `modified_by`, `version`, `is_delete`, `tenant_code`, `name`, `edition`, `remark`, `uri`, `type`) VALUES ('e33b4cd3889a9b7b61783d1579c4369d', '2020-10-31 17:04:39', '2020-10-31 17:05:08', '5f8c92f4841235a48e498520cea74a64', '5f8c92f4841235a48e498520cea74a64', 1, 0, NULL, '2.1', '2.1', '样式修改', 'group101/FD/wKgB-l-dJ46AYFwaAeEdUGM_L9Q514.apk', 'laoye');
INSERT INTO `app`(`id`, `created_at`, `modified_at`, `created_by`, `modified_by`, `version`, `is_delete`, `tenant_code`, `name`, `edition`, `remark`, `uri`, `type`) VALUES ('03ec02bfeac2d92efeca54758e402afb', '2020-10-30 14:20:54', '2020-10-30 14:41:47', '20f748ce6c4c5e3771d080ceb220ca8b', '20f748ce6c4c5e3771d080ceb220ca8b', 2, 0, NULL, '爷', '2.0', '上线', 'group1/M00//wKgB-l-br7uAOivMAeEb0L11RzE765.apk', 'laoye');
INSERT INTO `app`(`id`, `created_at`, `modified_at`, `created_by`, `modified_by`, `version`, `is_delete`, `tenant_code`, `name`, `edition`, `remark`, `uri`, `type`) VALUES ('96b31b5bc8437a020ef13181c8b28e53', '2020-10-30 13:58:44', '2020-10-30 14:12:41', '05406b5219a34e04ab32e666f297c37a', '20f748ce6c4c5e3771d080ceb220ca8b', 1, 0, NULL, '爷', '1.1.4', '类型判断', 'group1/1/CE/wKgB-l-bqouABTYZAeEbvOPMqRk053.apk', 'laoye');
INSERT INTO `app`(`id`, `created_at`, `modified_at`, `created_by`, `modified_by`, `version`, `is_delete`, `tenant_code`, `name`, `edition`, `remark`, `uri`, `type`) VALUES ('60b116c2ab09fb6f71235861a2501031', '2020-10-30 10:11:22', '2020-10-30 11:40:06', '20f748ce6c4c5e3771d080ceb220ca8b', '05406b5219a34e04ab32e666f297c37a', 1, 0, NULL, '爷', '1.1.2', '新增头像', 'group1/07/wKgB-l-bdUCAFLFgAeEWDLRCmmQ745.apk', 'laoye');

2、实体类

/**
 * App
 * @author 111
 */
@ApiModel(value = "App", description = "App")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@Accessors(chain = true)
@TableName(value = "app")
public class App extends StringSuperEntity<App> {

    /** 名称 */
    @ApiModelProperty(value = "名称", position = 10, required = true)
    @Column(length = 32)
    private String name;

    /** 版本 */
    @ApiModelProperty(value = "版本", position = 12, required = true)
    @Column(length = 32)
    private String edition;

    /** 备注 */
    @ApiModelProperty(value = "备注", position = 14)
    @Column(length = 300)
    private String remark;

    /** 附件路径 */
    @ApiModelProperty(value = "附件路径", position = 16, required = true)
    @Column(length = 150)
    private String uri;

    /** 类型 */
    @ApiModelProperty(value = "类型", position = 18)
    @Column(length = 100)
    private String type;

   /** 主键 */
    @ApiModelProperty(value = "主键", position = 1)
    private PK id;

    /** 创建时间 */
    @ApiModelProperty(value = "创建时间", position = 2)
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    /** 更新时间 */
    @ApiModelProperty(value = "更新时间", position = 3)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime modifiedAt;

    /** 创建人 */
    @Column(length = 32)
    @ApiModelProperty(value = "创建人", position = 4)
    @TableField(fill = FieldFill.INSERT)
    private String createdBy;

    /** 更新人 */
    @Column(length = 32)
    @ApiModelProperty(value = "更新人", position = 5)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String modifiedBy;

    /** 版本(乐观锁) */
    @ApiModelProperty(value = "版本(乐观锁)", position = 6)
    @Version
    @TableField(fill = FieldFill.INSERT)
    private Integer version;

    /** 逻辑删除 */
    @ApiModelProperty(value = "逻辑删除", position = 7)
    @TableField(fill = FieldFill.INSERT)
    private BooleanType isDelete;
}

3、三层代码
controller

    /**
     * 获取最新App信息
     * @param type 类型
     * @return App VO
     */
    @ApiOperation(value = "获取最新App信息", notes = "获取最新App信息")
    @GetMapping("/{type}/fresh")
    ResponseInfo<AppVO> fresh(@ApiParam(value = "type", required = true) @PathVariable String type);

controllerimpl

    @Override
    public ResponseInfo<AppVO> fresh(String type) {
        String version = sortVersion(type);
        App app = appService.selectOne(this.appService.createQueryWrapper().lambda().eq(App::getEdition,version));
        return ResponseInfo.ofOkOrNoContent(BeanConverts.convert(app, AppVO.class));
    }

    public String sortVersion(String type){
        List<App> appVersionList = appService.selectList(this.appService.createQueryWrapper().lambda().eq(App::getType,type));
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for (int i = 0 ;i <= appVersionList.size()-1;i++){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("compareKey", appVersionList.get(i).getEdition());
            list.add(map);
        }
        return AppVersionCompare.sort(list).get(list.size()-1).get("compareKey").toString();
    }

AppVersionCompare


import java.util.*;

public class AppVersionCompare implements Comparator<Map<String, Object>> {

    /**
     * 排序用到的key
     */
    private String key;

    public AppVersionCompare(String key) {
        this.key = key;
    }

    @Override
    public int compare(Map<String, Object> o1, Map<String, Object> o2) {
        // 获取比较的字符串
        String compareValue1 = (String) o1.get(key);
        String compareValue2 = (String) o2.get(key);
        String[] valueSplit1 = compareValue1.split("[.]");
        String[] valueSplit2 = compareValue2.split("[.]");
        int minLength = valueSplit1.length;
        if (minLength > valueSplit2.length) {
            minLength = valueSplit2.length;
        }
        for (int i = 0; i < minLength; i++) {
            int value1 = Integer.parseInt(valueSplit1[i]);
            int value2 = Integer.parseInt(valueSplit2[i]);
            if(value1 > value2){
                return 1;
            }else if(value1 < value2){
                return -1;
            }
        }
        return valueSplit1.length - valueSplit2.length;
    }

    public static List<Map<String,Object>> sort(List<Map<String,Object>> list) {
        Collections.sort(list, new AppVersionCompare("compareKey"));
        return list;
    }
}

获取到最新版本
在这里插入图片描述
排序之前的版本
在这里插入图片描述
排序后的版本
在这里插入图片描述

二、app版本比较大小

    /**
     *  比较两个版本号的大小,如果version1大于version2,返回值为1,等于为0,小于为-1
     *  例如:1.2.4和1.1.100比较大小,应是前者大于后者!
     * @param version1 第一个版本号
     * @param version2 目标版本号
     * @return
     */
    public static Integer compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }
        String[] version1Array = version1.split("[._]");
        String[] version2Array = version2.split("[._]");
        int index = 0;
        int minLen = Math.min(version1Array.length, version2Array.length);
        long diff = 0;

        while (index < minLen
                && (diff = Long.parseLong(version1Array[index])
                - Long.parseLong(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            for (int i = index; i < version1Array.length; i++) {
                if (Long.parseLong(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Long.parseLong(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值