排序Bean的Arraylist的Arraylist [英] Sorting Arraylist of Arraylist of Bean
问题描述
我有一个 ArrayList
的 ArrayList
的Bean和我需要排序这个 ArrayList
I have an ArrayList
of ArrayList
of Bean and I need to sort this ArrayList
according to the date
variable in Bean.
ArrayList<ArrayList<DriverLogDatePair>> driverLogList
并且 DriverLogDatePair
code> DateTime date ,它还有一个 compareTo
方法。
And DriverLogDatePair
bean have a variable DateTime date
, it also has a compareTo
method.
public int compareTo(DriverLogDatePair o) {
return date.compareTo(o.date);
}
但我不能排序 driverLogList
。
But I am not able to sort driverLogList
.
推荐答案
您必须填写此代码:
ArrayList<ArrayList<DriverLogDatePair>> driverLogList = new ArrayList<>();
Collections.sort( driverLogList, new Comparator<ArrayList<DriverLogDatePair>>(){
@Override public int compare(
ArrayList<DriverLogDatePair> left,
ArrayList<DriverLogDatePair> right )
{
return 0;
}});
因为第一个数组包含一个包含Comparable的数组。
您提供的比较器是为DriverLogDatePair而不是ArrayList< DriverLogDatePair>
Because the first array contains an array which contains a Comparable. The comparator you provide is for DriverLogDatePair not for ArrayList< DriverLogDatePair >
(...在此帖子的评论...)
(... After comments of this post... )
完成比较我建议:
int size = left.size();
int diff = size - right.size();
if( diff != 0 ) return diff;
for( int i = 0; i < size; ++i ) {
diff = left.get( i ).compareTo( right.get(i) );
if( diff != 0 ) return diff;
}
但我不知道这个比较的真正含义。
这是一个语义问题,这是真的你想要什么?
But I have no idea of the true meaning of this comparison. It's a semantic problem, is this really what you want?
这篇关于排序Bean的Arraylist的Arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!