含有对象的List集合实现字母数字混合排序
List<PageData> varList = [{BOMCode=10A, mantotal=4}, {BOMCode=10B, mantotal=1}, {BOMCode=11A, mantotal=1}, {BOMCode=11B, mantotal=1}, {BOMCode=12A, mantotal=1}, {BOMCode=1A, mantotal=9}, {BOMCode=2A, mantotal=168}, {BOMCode=2B, mantotal=57}, {BOMCode=3A, mantotal=440}, {BOMCode=3B, mantotal=363}, {BOMCode=4A, mantotal=317}, {BOMCode=4B, mantotal=612}, {BOMCode=5A, mantotal=175}, {BOMCode=5B, mantotal=188}, {BOMCode=6A, mantotal=99}, {BOMCode=6B, mantotal=132}, {BOMCode=7A, mantotal=48}, {BOMCode=7B, mantotal=58}, {BOMCode=8A, mantotal=22}, {BOMCode=8B, mantotal=10}, {BOMCode=9A, mantotal=7}, {BOMCode=9B, mantotal=3}]
现在要对集合对象里面的BOMCode做排序,返回一个排序后的varList:
Collections.sort(varList, new Comparator<PageData>(){
/*
* int compare(PageData pd1, PageData pd2) 返回一个基本类型的整型,
* 返回负数表示:pd1 小于pd2,
* 返回0 表示:pd1和pd2相等,
* 返回正数表示:pd1大于pd2。
*/
@Override
public int compare(PageData pd1, PageData pd2) {
int number1;
int number2;
int ascii1 = 0;
int ascii2 = 0;
String s1 = pd1.getString("BOMCode");
String s2 = pd2.getString("BOMCode");
// 获取每个数字的最后一位,判断是否为大写字母(判断其ASCII码是否在65到90之间)
if ((int) (s1.toCharArray()[s1.length() - 1]) >= 65
&& (int) (s1.toCharArray()[s1.length() - 1]) <= 90) {
number1 = Integer.parseInt(s1.substring(0,
s1.length() - 1));
ascii1 = (int) (s1.toCharArray()[s1.length() - 1]);
} else {
number1 = Integer.parseInt(s1);
}
if ((int) (s2.toCharArray()[s2.length() - 1]) >= 65
&& (int) (s2.toCharArray()[s2.length() - 1]) <= 90) {
number2 = Integer.parseInt(s2.substring(0,
s2.length() - 1));
ascii2 = (int) (s2.toCharArray()[s2.length() - 1]);
} else {
number2 = Integer.parseInt(s2);
}
// 从小到大排序
if (number1 > number2) {
return 1;
} else if (number1 == number2) {// 数值相等则判断是否有字母以及字母的顺序
// 如果两个都有字母,则判断顺序
// 按ASCII码从小到大排列(即从A到Z排列)
if (ascii1 < ascii2) {
return 1;
}
}
return -1;
}
});
return varList;
最后返回的varList如下:
[{BOMCode=1A, mantotal=9}, {BOMCode=2B, mantotal=57}, {BOMCode=2A, mantotal=168}, {BOMCode=3B, mantotal=363}, {BOMCode=3A, mantotal=440}, {BOMCode=4B, mantotal=612}, {BOMCode=4A, mantotal=317}, {BOMCode=5B, mantotal=188}, {BOMCode=5A, mantotal=175}, {BOMCode=6B, mantotal=132}, {BOMCode=6A, mantotal=99}, {BOMCode=7B, mantotal=58}, {BOMCode=7A, mantotal=48}, {BOMCode=8B, mantotal=10}, {BOMCode=8A, mantotal=22}, {BOMCode=9B, mantotal=3}, {BOMCode=9A, mantotal=7}, {BOMCode=10B, mantotal=1}, {BOMCode=10A, mantotal=4}, {BOMCode=11B, mantotal=1}, {BOMCode=11A, mantotal=1}, {BOMCode=12A, mantotal=1}]
含有对象的List集合实现字母数字混合排序的更多相关文章
- 对文本行进行排序,新增-d(目录排序),只对字母数字空格排序(TCPL 练习5-16)
文本行的排序用到了命令行参数以及多级指针,在要求只对字母数字空格进行排序时,关键的问题点是兼容-f命令参数,也就是排序的同时忽略大小写.由于在之前的练习中,我将忽略大小写的比较方法重新写了一个函数tr ...
- PHP实现字母数字混合验证码
一款简单的PHP实现字母数字混合验证码,支持自定义验证码.验证码图片.宽度.高度.个数.背景图片等 验证码调用地址:Application\Home\Controller\CodeController ...
- MYSQL数据库字母数字混合字段排序问题
对MySQL数据表里的一个字符型字段排序,其内容格式为一位字母+顺序数字.数字没有前导零,长度不固定.这种含字母的数字序列,排序出来的结果和我们想要的结果是不一样的,因为它不是纯数字,只能按字符规则排 ...
- mysql如何给字母数字混合的字段排序?
mysql> select * from t_SpiritInside; +------+ | col | +------+ | s1 | | s2 | | s11 | | s12 ...
- oracle数据库表中,插入数据的时候如何产生一个 字母+数字 编号?
Oracle 语句中“||”代表什么啊? oracle数据库表中,插入数据的时候如何产生一个 字母+数字 编号? 排序的话,用order by来处理即可.比如:cola123a234b999b335s ...
- js 正则 以字母开头必须有 大小写字母数字组成 可以有“@"或 ”.“
js 正则 以字母开头必须有 大小写字母数字组成 可以有“@"或 ”.“ var reg = /^[a-zA-Z]{1}(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d_@ ...
- js随机生成字母数字组合的字符串 随机动画数字
效果描述: 附件中只有一个index.html文件有效 其中包含css以及html两部分内容 纯js生成的几个随机数字 每次都不重复,点击按钮后再次切换 使用方法: 1.将css样式引入到你的网页中 ...
- 字母数字、字母、汉字验证码 (java)
原文:http://blog.csdn.net/qh_java/article/details/49854477 一.字母数字,字母,汉字验证码的生成代码 1.字母数字验证码: package com ...
- MongoDB学习笔记~自己封装的Curd操作(查询集合对象属性,更新集合对象)
回到目录 我不得不说,mongodb官方驱动在与.net结合上做的不是很好,不是很理想,所以,我决定对它进行了二次封装,这是显得很必然了,每个人都希望使用简单的对象,而对使用复杂,麻烦,容易出错的对象 ...
随机推荐
- keystone验证安装
以管理员的身份来请求鉴权的标识 keystone --os-tenant-name admin --os-username admin --os-password 123456 --os-auth- ...
- 为什么你的javascript学了这么久,水平还是烂成了渣?
今年我给公司面试时,面试了百来个人,水平我就呵呵了,还觉得自己学了很久很厉害了,其实呢,渣的很呀,这篇文章送给想学好javascript找份工作的同学们. 首先要说明的是,咱现在不是高手,最多还是一个 ...
- JProfiler> ERROR: Invalid license key. Aborting.
用IDEA+Tomcat的方式打开JProfiler,出现错误 1,Event Log 出错 16:10 Application Server was not connected before run ...
- Spring boot项目集成Neo4j
第一步,创建Springboot工程 使用Eclipse 创建Maven项目,并修改pom.xml文件为: <?xml version="1.0" encoding=&quo ...
- 数据分析画图,使用原生sql查询数据
1.使用工具 https://www.hcharts.cn/ http://echarts.baidu.com/ 2.子表查询 id 创建时间 内容 处理者 1 2017-02-01 11:11 1 ...
- luogu P5329 [SNOI2019]字符串
传送门 显然要写一个排序,那只要考虑cmp函数怎么写就行了.第\(i\)个字符串和第 \(j\)个,首先前\(min(i,j)-1\)个字符是相同的,然后就是要比较后缀\(min(i,j)\)和\(m ...
- 前端Unicode字符图标
前端Unicode字符图标 原文链接地址:http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/20141225979.html
- Huawei交换机路由器远程Telnet配置
<huawei>system-view Enter system view, return user view with Ctrl+Z.[huawei]interface g0/0/0[h ...
- centos 7 安装相关环境
How To Create a Sudo User on CentOS 7 https://www.digitalocean.com/community/tutorials/how-to-create ...
- 牛客练习赛14 A n的约数 (数论)
链接:https://ac.nowcoder.com/acm/contest/82/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288 ...