我有List<Bean>
,其中我的对象值为String
。这些字符串看起来像这样#34;这是1&#34;,&#34;这是10&#34;,&#34;这是2&#34;,&#34;这是11&#34;
我的问题是如何使用番石榴按顺序排列它们的顺序为1,2,10,11?
List<Bean> orderedList = new ArrayList<>();
Function<Bean, String> getNameFunction = new Function<Bean, String>() {
@Override
public String apply(Bean from) {
return from.getShape();
}
};
Ordering<Bean> nameOrdering = Ordering.natural().onResultOf(getNameFunction);
ImmutableSortedSet<Bean> sortedShapes = ImmutableSortedSet.orderedBy(
nameOrdering).addAll(lb).build();
orderedList.addAll(sortedShapes);
return orderedList;
这不像我想要的那样对它们进行排序,如1,10,11,2 你能帮帮我吗? 谢谢!
答案 0 :(得分:2)
Java中String
的自然顺序是词典。您需要创建或找到满足您需求的Comparator
实现。如果你自己编写它,你可以改为创建Ordering
的子类。然后你可以这样做:
Ordering<Bean> nameOrdering = myStringOrdering.onResultOf(getNameFunction);
答案 1 :(得分:1)
Java
Strings
的默认(自然)排序为lexicographical,这意味着Strings
按字母顺序排列组件字母(字符)按字符比较)。来自String.compareTo
docs:
[...]如果他们(字符串)在一个或多个索引位置有不同的字符, 设k是最小的索引;然后是位置k处的字符具有
的字符串 较小的值,通过使用&lt;运算符,按字典顺序排列 在另一个字符串之前。
正如ColinD建议您需要实现支持Comparator
的字母数字排序的Strings
。这是一个例子:
List<Bean> lb = new ArrayList<Bean>() {{
add(new Bean("This is 1"));
add(new Bean("This is 10"));
add(new Bean("This is 11"));
add(new Bean("This is 2"));
}};
final AlphaNumericComparator comparator= new AlphaNumericComparator();
Ordering<Bean> nameOrdering = new Ordering<Bean>() {
@Override public int compare(Bean left, Bean right) {
return comparator.compare(left.getShape(),right.getShape());
}
};
ImmutableSortedSet<Bean> sortedShapes = ImmutableSortedSet.orderedBy(nameOrdering).addAll(lb).build();
List<Bean> orderedList = new ArrayList<>();
orderedList.addAll(sortedShapes);
System.out.println("orderedList:" + orderedList);
AlphaNumericComparator
(Pierre-Luc Paour饰演):
public class AlphaNumericComparator implements Comparator {
int compareRight(String a, String b) {
int bias = 0;
int ia = 0;
int ib = 0;
for (; ; ia++, ib++) {
char ca = charAt(a, ia);
char cb = charAt(b, ib);
if (!Character.isDigit(ca) && !Character.isDigit(cb))
return bias;
else if (!Character.isDigit(ca))
return -1;
else if (!Character.isDigit(cb))
return +1;
else if (ca < cb)
if (bias == 0)
bias = -1;
else if (ca > cb)
if (bias == 0)
bias = +1;
else if (ca == 0 && cb == 0)
return bias;
}
}
public int compare(Object o1, Object o2) {
String a = o1.toString();
String b = o2.toString();
int ia = 0, ib = 0;
int nza = 0, nzb = 0;
char ca, cb;
int result;
while (true) {
nza = nzb = 0;
ca = charAt(a, ia);
cb = charAt(b, ib);
while (Character.isSpaceChar(ca) || ca == '0') {
if (ca == '0')
nza++;
else
nza = 0;
ca = charAt(a, ++ia);
}
while (Character.isSpaceChar(cb) || cb == '0') {
if (cb == '0')
nzb++;
else
nzb = 0;
cb = charAt(b, ++ib);
}
if (Character.isDigit(ca) && Character.isDigit(cb))
if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0)
return result;
if (ca == 0 && cb == 0)
return nza - nzb;
if (ca < cb)
return -1;
else if (ca > cb)
return +1;
++ia;
++ib;
}
}
static char charAt(String s, int i) {
if (i >= s.length()) {
return 0;
} else
return s.charAt(i);
}
}