package cn.cnnho.backstage.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import cn.cnnho.backstage.test.User;
import cn.cnnho.backstage.vo.file.OriginalFileModel;

public class StringComparatorUtils implements Comparator<User>{
private String str1, str2;
private int pos1, pos2, len1, len2;

public int compare(Users1, Users2)
{
str1 = s1.getName();
str2 = s2.getName();
len1 = str1.length();
len2 = str2.length();
pos1 = pos2 = 0;

int result = 0;
while (result == 0 && pos1 < len1 && pos2 < len2)
{
char ch1 = str1.charAt(pos1);
char ch2 = str2.charAt(pos2);

if (Character.isDigit(ch1))
{
result = Character.isDigit(ch2) ? compareNumbers() : -1;
}
else if (Character.isLetter(ch1))
{
result = Character.isLetter(ch2) ? compareOther(true) : 1;
}
else
{
result = Character.isDigit(ch2) ? 1
: Character.isLetter(ch2) ? -1
: compareOther(false);
}

pos1++;
pos2++;
}

return result == 0 ? len1 - len2 : result;
}

private int compareNumbers()
{
int end1 = pos1 + 1;
while (end1 < len1 && Character.isDigit(str1.charAt(end1)))
{
end1++;
}
int fullLen1 = end1 - pos1;
while (pos1 < end1 && str1.charAt(pos1) == '0')
{
pos1++;
}

int end2 = pos2 + 1;
while (end2 < len2 && Character.isDigit(str2.charAt(end2)))
{
end2++;
}
int fullLen2 = end2 - pos2;
while (pos2 < end2 && str2.charAt(pos2) == '0')
{
pos2++;
}

int delta = (end1 - pos1) - (end2 - pos2);
if (delta != 0)
{
return delta;
}

while (pos1 < end1 && pos2 < end2)
{
delta = str1.charAt(pos1++) - str2.charAt(pos2++);
if (delta != 0)
{
return delta;
}
}

pos1--;
pos2--;

return fullLen2 - fullLen1;
}

private int compareOther(boolean isLetters)
{
char ch1 = str1.charAt(pos1);
char ch2 = str2.charAt(pos2);

if (ch1 == ch2)
{
return 0;
}

if (isLetters)
{
ch1 = Character.toUpperCase(ch1);
ch2 = Character.toUpperCase(ch2);
if (ch1 != ch2)
{
ch1 = Character.toLowerCase(ch1);
ch2 = Character.toLowerCase(ch2);
}
}
return ch1 - ch2;
}
public static void main(String[] args) {
//list集合
// List<User> list = new ArrayList<User>();
// User user = new User();
// user.setName("1.jpg");
// user.setAge("12");
// User user1 = new User();
// user1.setName("10.jpg");
// user1.setAge("13");
// User user2 = new User();
// user2.setName("2.jpg");
// user2.setAge("14");
// User user3 = new User();
// user3.setName("2a2.jpg");
// user3.setAge("15");
// list.add(user);
// list.add(user1);
// list.add(user2);
// list.add(user3);
// List<User> result = list.stream().sorted(new StringComparatorUtils()).collect(Collectors.toList());
//
//
// Collections.sort(list,new StringComparatorUtils());
// for(int i = 0;i < result.size();i++){
// System.out.println(result.get(i).getName().toString());
// }
//数组
// String arr[] = {"1.jpg", "12.jpg", "10.jpg", "2.jpg", "2abc.jpg","a2bc.jpg","a1bc.jpg"};
//
// Arrays.sort(arr, new StringComparatorUtils());
// for(int i=0;i<arr.length;i++){
// System.out.println(arr[i]);
// }
}

}

最新文章

  1. opengl 笔记(1)
  2. Android自动化压力测试之Monkey Test 异常解读(五)
  3. Allegro学习(http://www.asmyword.com/forum.php?mod=forumdisplay&amp;fid=86)
  4. Android Bundle类
  5. VideoView
  6. C# 仿制QQ弹出新闻消息框
  7. centos 安装openssl 以及库文件
  8. Ext.Net学习笔记12:Ext.Net GridPanel Filter用法
  9. SQL Server 性能优化之——重复索引
  10. 在Eclipse中执行Andorid test preject提示The connection to adb is down, and a severe error has occured.解决方法
  11. html学习中
  12. [FreeRadius2]遇到问题记录
  13. 内存数据网格hazelcast的一些机制原理
  14. Mui Webview下来刷新上拉加载实现
  15. 关于 Docker 镜像的操作,看完这篇就够啦 !(上)
  16. Animate与transform的使用
  17. DotNet 资源大全(转)
  18. HTML语义化
  19. Notes of Daily Scrum Meeting(12.20)
  20. Perl入门

热门文章

  1. Json实体类驼峰名称转化器
  2. scanf与正则表达式的搭配及应用
  3. asp.net数据库增删改查demo
  4. [洛谷Luogu]P1803 线段覆盖问题
  5. Oracle Exadata 学习笔记之核心特性Part1
  6. 155-PHP stripos函数
  7. Swift 类的使用class
  8. ACM-可乐兑换
  9. HDU 5477: A Sweet Journey
  10. Vue 获取时间戳返回自定义时间格式