这个比较器写的比较好,多种数据形式也能比对排序。
package code;
import java.util.*;
public class IntuitiveStringComparator<T extends CharSequence> implements
Comparator<T> {
private T str1, str2;
private int pos1, pos2, len1, len2;
public int compare(T s1, T s2) {
str1 = s1;
str2 = s2;
len1 = str1.length();
len2 = str2.length();
pos1 = pos2 = 0;
if (len1 == 0) {
return len2 == 0 ? 0 : -1;
} else if (len2 == 0) {
return 1;
}
if (isNumeric(str1.toString()) && isNumeric(str2.toString())) {
// System.out.println("test ");
if (Double.parseDouble(str1.toString()) > Double.parseDouble(str2
.toString())) {
return 1;
} else {
return -1;
}
}else {
while (pos1 < len1 && pos2 < len2) {
char ch1 = str1.charAt(pos1);
char ch2 = str2.charAt(pos2);
int result = 0;
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);
}
if (result != 0) {
return result;
}
}
}
return len1 - len2;
}
private int compareNumbers() {
int delta = 0;
int zeroes1 = 0, zeroes2 = 0;
char ch1 = (char) 0, ch2 = (char) 0;
// Skip leading zeroes, but keep a count of them.
while (pos1 < len1 && (ch1 = str1.charAt(pos1++)) == '0') {
zeroes1++;
}
while (pos2 < len2 && (ch2 = str2.charAt(pos2++)) == '0') {
zeroes2++;
}
// If one sequence contains more significant digits than the
// other, it's a larger number. In case they turn out to have
// equal lengths, we compare digits at each position; the first
// unequal pair determines which is the bigger number.
while (true) {
boolean noMoreDigits1 = (ch1 == 0) || !Character.isDigit(ch1);
boolean noMoreDigits2 = (ch2 == 0) || !Character.isDigit(ch2);
if (noMoreDigits1 && noMoreDigits2) {
return delta != 0 ? delta : zeroes1 - zeroes2;
} else if (noMoreDigits1) {
return -1;
} else if (noMoreDigits2) {
return 1;
} else if (delta == 0 && ch1 != ch2) {
delta = ch1 - ch2;
}
ch1 = pos1 < len1 ? str1.charAt(pos1++) : (char) 0;
ch2 = pos2 < len2 ? str2.charAt(pos2++) : (char) 0;
}
}
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 boolean isNumeric(String str12) {
// String str12 = str1.toString();
try {
double d = Double.parseDouble(str12);
} catch (NumberFormatException nfe) {
return false;
}
return true;
// return str12.matches("-?\\d+(\\.\\d+)?"); //match a number with
// optional '-' and decimal.
}
public static void main(String[] args) {
String[] list = { "foo 03", "foo 00003", "foo 5", "foo 003", "foo~03",
"foo 10far", "foo 10boo", "2", "0.5", "foo 10", "-20", "1.1", "foo!03",
"1.0", "10000", "20", ".7","-40"
};
Arrays.sort(list, new IntuitiveStringComparator<String>());
for (String s : list) {
System.out.println(s);
}
}
}
运行结果如下:
-40
-20
0.5
.7
1.0
1.1
2
20
10000
foo 03
foo 003
foo 00003
foo 5
foo 10
foo 10far
foo 10boo
foo!03
foo~03
即使小数点前没有零也能正常排序。
分享到:
相关推荐
程序实现时间排序Comparator
java排序Comparator和Comparable
Java中Comparator接口的用法 对集合排序的例子 Comparator
List对象集合的排序:比较器Comparator
python库。 资源全名:number_comparator-0.0.3b1-py3-none-any.whl
可以通过两种方式自定义对象的属性大小进行比较,对一个list或数组对根据属性排序一个list或数组
4. * 汉字按照拼音排序的比较器 5. * @author KennyLee 2009-2-23 10:08:59 6. * 7. */ 8.public class PinyinComparator implements Comparator<Object> { 9. public int compare(Object o1, Object o2) { 10...
主要介绍了JAVA使用Comparator接口实现自定义排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://singhoo.iteye.com/blog/1704428
java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子
要注意的是List,Set,Queue继承了Collection接口,...这里想用一个简单的例子展示一下他们的使用,内容包括:List、Map、Set、Queue,Collections、Comparable与Comparator,排序、搜索,内部类,泛型、重写equals、hashCode
在Java中对于字符串数组的排序,我们可以使用Arrays.sort(String[])方法很便捷的进行排序。例如: String[] arrays = new String[] { gyu, sdf, zf, 大同, 收到, 地方, 三等分, 的人, 反对高铁, 泛代数, 上的投入...
由Comparator实现类对象负责集合元素的排序逻辑。 // 自定义比较器 class MyTreeSetCompartor implements Comparator { // 实现compare方法 @Override public int compare(Object o1, ...
在这篇教程里,我们将要去了解下即将到来的JDK 8(译注,现在JDK 8已经发布了)中的Lambda表达式——特别是怎样使用它来编写Comparator和对集合(Collection)进行排序。 这篇文章是Baeldung上的“Java ——...
Comparable&Comparator区别,看完就明白了
文件比對工具,可以生成比對報告,支持itd、doc和ttx。
主要介绍了Java8 Comparator排序方法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
comparator接口与Comparable接口的区别