使用番石榴订购的对象列表的多标准排序 [英] Multi criteria sorting of a list of objects with Guava Ordering
问题描述
我有一个类不能实现类比,但需要根据2个字段进行排序。
让我们说这个类是
class X {
String stringValue;
java.util.Date dateValue;
}
我有这些列表
列表< X> lotsOfX;
我想先根据值字段对它们进行排序,然后根据每个'组中的dateValue降序'b'b
到目前为止,我一直在做的是
列表与LT; X> sortedList = ImmutableList.copyOf(Ordering.natural()。onResultOf(dateValueSortFunction).reverse()。sortedCopy(lotsOfX));
sortedList = ImmutableList.copyOf(Ordering.natural()。onResultOf(stringValueSortFunction).sortedCopy(sortedList));
函数被定义为:
public class DateValueSortFunction< X>实现函数< X,Long> {
@Override
public Long apply(X input){
return input.getDateValue()。getTime(); //返回毫秒时间
}
}
和
public class StringValueSortFunction< X>实现函数< X,Integer> {
@Override
public Integer apply(X input){
if(input.getStringValue()。equalsIgnoreCase(Something))
return 0;
else if(input.getStringValue()。equalsIgnoreCase(Something else))
return 1;
else
return 2;
$ b $ p
$ b 在sortedList中的预期输出是
Something 03/18/2013
Something 03/17/2013
Something else 03/20/2013
Something else 2013年3月19日
....
我的方法可行,但显然效率很低遍历列表两次。有没有更好的方式做到这一点?
注意:在GWT应用程序中使用它。实现可比性不是一种选择。
解决方案我怀疑你想要 Ordering.compound
。您可以在一个声明中完成所有操作,但我会使用:
排序< X> primary = Ordering.natural()。onResultOf(stringValueSortFunction);
订购< X> secondary = Ordering.natural()
.onResultOf(dateValueSortFunction)
.reverse();
订购< X> compound = primary.compound(secondary);
列表< X> sortedList = compound.immutableSortedCopy(lotsOfX);
I have a class WHICH CANNOT implement comparable, but needs to be sorted based on 2 fields. How can I achieve this with Guava?
Let's say the class is
class X {
String stringValue;
java.util.Date dateValue;
}
and I have a list of these
List<X> lotsOfX;
I want to sort them based on the value field first and then based on dateValue descending within each 'group' of 'value' fields.
What I have been doing so far is
List<X> sortedList = ImmutableList.copyOf(Ordering.natural().onResultOf(dateValueSortFunction).reverse().sortedCopy(lotsOfX));
sortedList = ImmutableList.copyOf(Ordering.natural().onResultOf(stringValueSortFunction).sortedCopy(sortedList));
The functions are defined as:
public class DateValueSortFunction<X> implements Function<X, Long> {
@Override
public Long apply(X input) {
return input.getDateValue().getTime(); //returns millis time
}
}
and
public class StringValueSortFunction<X> implements Function<X, Integer> {
@Override
public Integer apply(X input) {
if(input.getStringValue().equalsIgnoreCase("Something"))
return 0;
else if(input.getStringValue().equalsIgnoreCase("Something else"))
return 1;
else
return 2;
}
}
Expected output in sortedList is
Something 03/18/2013
Something 03/17/2013
Something else 03/20/2013
Something else 03/19/2013
....
My approach works but is obviously inefficient for traversing the list twice. Is there a better way of doing this?
Note: Using this in a GWT app. Implementing comparable is not an option.
解决方案 I suspect you want Ordering.compound
. You could do it all in one statement, but I'd use:
Ordering<X> primary = Ordering.natural().onResultOf(stringValueSortFunction);
Ordering<X> secondary = Ordering.natural()
.onResultOf(dateValueSortFunction)
.reverse();
Ordering<X> compound = primary.compound(secondary);
List<X> sortedList = compound.immutableSortedCopy(lotsOfX);
这篇关于使用番石榴订购的对象列表的多标准排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!