假设我有
final Iterable<String> unsorted = asList("FOO", "BAR", "PREFA", "ZOO", "PREFZ", "PREFOO");我可以做些什么将这个未排序的列表转换成这个列表:
[PREFZ, PREFA, BAR, FOO, PREFOO, ZOO](该列表以必须首先出现的已知值开头(此处为"PREFA“和"PREFZ"),其余值按字母顺序排序)
我认为在芭乐中有一些有用的类可以完成这项工作(排序、谓词……),但我还没有找到解决方案……
发布于 2010-06-24 21:37:10
我也会使用Collections.sort(list),但我想我会使用一个比较器,在比较器中你可以定义你自己的规则,例如
class MyComparator implements Comparator<String> {
public int compare(String o1, String o2) {
// Now you can define the behaviour for your sorting.
// For example your special cases should always come first,
// but if it is not a special case then just use the normal string comparison.
if (o1.equals(SPECIAL_CASE)) {
// Do something special
}
// etc.
return o1.compareTo(o2);
}
}然后按以下操作进行排序:
Collections.sort(list, new MyComparator());https://stackoverflow.com/questions/3110265
复制相似问题