需求说明
有时候人名排序需要按照笔画来排序,原理其实很简单,把每个汉字的笔画数出来、排序的序号列出来就可以了,因为也就近2万条记录,问题不大。
我把《汉字笔画排序数据库表》分享出来:https://github.com/yaoxinghuo/yaoxinghuo.github.io/blob/master/resources/t_stroke.sql
导入到的数据库表:t_stock,数据结构和数据示例如下:
有了这个数据库,需要笔画排序的表,再新增一个字段,把姓名用t_stroke查出来排序(t_stroke的c字段)存到数据库表里去,
如果有姓名有3个字,可以根据业务实际需求来做,例如可以存一个15位的排序字段(每个汉字5位作为排序)
示例Java代码如下:
String name = "张三丰";
char[] chars = name.toCharArray();
int length = chars.length > 2 ? 3 : chars.length;
long stokeIndex = 0;
Stoke stoke0 = stokeDao.selectStokeByn(String.valueOf(chars[0]));
stokeIndex = stokeIndex + (stoke0 == null ? 99999 : stoke0.getO()) * 10000000000L;
if (length > 2) {
Stoke stoke1 = stokeDao.selectStokeByn(String.valueOf(chars[1]));
stokeIndex = stokeIndex + (stoke1 == null ? 99999 : stoke1.getO()) * 100000L;
Stoke stoke2 = stokeDao.selectStokeByn(String.valueOf(chars[2]));
stokeIndex = stokeIndex + (stoke2 == null ? 99999 : stoke2.getO()) * 1L;
} else {
if (length == 1) {
stokeIndex += 1 * 100000L;
} else if (length == 2) {
Stoke stoke1 = stokeDao.selectStokeByn(String.valueOf(chars[1]));
stokeIndex = stokeIndex + (stoke1 == null ? 99999 : 1) * 100000L;
}
}
System.out.println("stokeIndex: " + stokeIndex);


文章评论