String[] k1 = {"0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1", "2.10", "2", "2.2", "2.1"};
double[] k2 = {0.10, 0.2, 0.1, 0, 1.10, 1.2, 1.1, 1, 2.10, 2, 2.2, 2.1};
Arrays.sort(k1);
Arrays.sort(k2);
System.out.println(Arrays.toString(k1));
System.out.println(Arrays.toString(k2));
输出:
[0, 0.1, 0.10, 0.2, 1, 1.1, 1.10, 1.2, 2, 2.1, 2.10, 2.2]
[0.0, 0.1, 0.1, 0.2, 1.0, 1.1, 1.1, 1.2, 2.0, 2.1, 2.1, 2.2]
我想要的,
[0, 0.1, 0.2, 0.10, 1, 1.1, 1.2, 1.10, 2, 2.1, 2.2, 2.10]
小数之前和之后的第一个排序。像
1、1.1、1.2、1.10、2、2.1 等。如何为此编写比较器?
2.1, 2.2, 2.10]
由于 2.10 按此顺序大于 2.2,因此它看起来像版本号排序:
import java.util.Arrays;
import java.util.Comparator;
public class VersionNumberComparator implements Comparator<String> {
@Override
public int compare(String version1, String version2) {
String[] v1 = version1.split("\.");
String[] v2 = version2.split("\.");
int major1 = major(v1);
int major2 = major(v2);
if (major1 == major2) {
return minor(v1).compareTo(minor(v2));
}
return major1 > major2 ? 1 : -1;
}
private int major(String[] version) {
return Integer.parseInt(version[0]);
}
private Integer minor(String[] version) {
return version.length > 1 ? Integer.parseInt(version[1]) : 0;
}
public static void main(String[] args) {
String[] k1 = { "0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1",
"2.10", "2", "2.2", "2.1" };
Arrays.sort(k1, new VersionNumberComparator());
System.out.println(Arrays.asList(k1));
}
}
package com.poc.sort;
/**
* @author voyger_india
*
*/
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
public class TestChapterSort {
public static void main(String[] args) {
List<String> chapters = new ArrayList<String>();
chapters.add("1.1");
chapters.add("1.2");
chapters.add("1");
chapters.add("1.3");
chapters.add("1.1.1");
chapters.add("5.6");
chapters.add("1.1.10");
chapters.add("4");
chapters.add("1.1.9");
chapters.add("1.2.1.10");
chapters.add("2.1.1.4.5");
chapters.add("1.2.1.9");
chapters.add("1.2.1");
chapters.add("2.2.2");
chapters.add("1.2.1.11");
TestChapterSort wer = new TestChapterSort();
System.out.println(wer.sortChapters(chapters));
}
private List<String> sortChapters(List<String> chapters) {
List<String> sortedChapters = new ArrayList<String>(0);
int index;
for (String currChapter : chapters) {
if (!sortedChapters.contains(currChapter)) {
index = getInsertIndex(sortedChapters, currChapter);
sortedChapters.add(index, currChapter);
System.out.println(sortedChapters);
}
}
return sortedChapters;
}
private int getInsertIndex(List<String> sortChapters, String currChapter) {
int insertIndex = 0;
if (!CollectionUtils.isEmpty(sortChapters)) {
int compChapterSub;
int currChapterSub;
String[] currChapterAr = currChapter.split("\.");
for (String compChapter : sortChapters) {
String[] compChapterAr = compChapter.split("\.");
for (int subLvl = 0; subLvl < 5; subLvl++) {
compChapterSub = parseToInt(compChapterAr, subLvl);
currChapterSub = parseToInt(currChapterAr, subLvl);
if (compChapterSub == currChapterSub) {
continue;
} else if (compChapterSub == 0 && currChapterSub == 0) {
break;
} else if (compChapterSub > currChapterSub) {
if (checkIfProper(subLvl, compChapterAr, currChapterAr)) {
return insertIndex;
}
}
}
insertIndex++;
}
} else {
return 0;
}
return insertIndex;
}
private int parseToInt(String[] subChapter, int subLvl) {
try {
return Integer.parseInt(subChapter[subLvl]);
} catch (ArrayIndexOutOfBoundsException ae) {
return 0;
}
}
private boolean checkIfProper(int subLvl, String[] compChapterAr, String[] currChapterAr) {
int subLvlCk = subLvl - 1;
int compChapterSub;
int currChapterSub;
while (subLvlCk > -1) {
compChapterSub = parseToInt(compChapterAr, subLvlCk);
currChapterSub = parseToInt(currChapterAr, subLvlCk);
if (compChapterSub < currChapterSub) {
return false;
}
subLvlCk--;
}
return true;
}
}
我认为你想要的是不可能的。考虑这个简单的例子:
public static void main(String[] args) {
double d = 0.1;
System.out.println(d);
d = 0.10;
System.out.println(d);
d = 0.100;
System.out.println(d);
}
指纹:
0.1
0.1
0.1
对于 Java 0.1,双精度与 0.10 和 0.100 相同。没有双0.1,0.10,0.100等,只有0.1
您可以编写一个 Comperator,首先将值转换为双精度并进行比较,如果它们相等,如"0.10"和"0.1",则应按长度比较它们。