1

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

And DriverLogDatePair bean have a variable DateTime date, it also has a compareTo method.

public int compareTo(DriverLogDatePair o) {
    return date.compareTo(o.date);
}

But I am not able to sort driverLogList.

6
  • Which ArrayList do you want to sort, the inner or outer one? You have a list of lists and you want to sort on a date element in the inner list, so I assume you want to sort the inner lists - or what? Commented Oct 31, 2012 at 8:46
  • How will the ArrayList<DriverLogDatePair> be sorted? You need to provide comparator for that too. Commented Oct 31, 2012 at 8:46
  • Do you want to sort all the individual lists? Or do you want to sort the global list (so change the order of the ArrayList<DriverLogDatePair>? If second, what criteria you want to use? Commented Oct 31, 2012 at 8:47
  • I need to sort the outer one on the basis of the bean date value. Commented Oct 31, 2012 at 8:51
  • you need to write your custom comparator and use it with the Collection.sort(list,newcomparator) method Commented Oct 31, 2012 at 8:52

4 Answers 4

3

You have to complete this code:

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;
   }});

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... )

At your request, to complete the comparator I suggest:

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Aubin can you please tell me what I need to put inside this. I mean how will I compare inside this.
Aubin I used you code and added for(int i=0;i<left.size();){ return left.get(i).getDate().compareTo(right.get(i).getDate()); } and it is working fine now.
0

The problem is that if you sort driverLogList it tries to compare the contained ArrayList objects. I would write a wrapper for that list:

public class DriverLogDatePairList extends ArrayList<DriverLogDatePair> implements Comparable<DriverLogDatePairList> {

    public int compareTo(DriverLogDatePairList o) {
        //your comparison criteria
    }
}

Then you can use it this way and sort it:

ArrayList<DriverLogDatePairList> driverLogList;

Comments

0

Generally, you can sort Lists using

Collections.sort( list , comparator )

So, assuming you want to sort the inner lists, iterate through the outer list and use that construct to sort the elements, after having implemented a fitting comparator:

Comparator comparator = 
    new Comparator<DateTime>()
    {
        @Override
        public int compare( DateTime o1, DateTime o2 )
        {
            return o1.compareTo( o2 );
        }
    }

Cheers,

Comments

0

Your compare method is seems wrong to me..

it should be like: (here i am assuming date as DriverLogDatePair Object )

public int compareTo(DriverLogDatePair o) {
    if(date.date < o.date) return -1;
    else if (date.date < o.date) return 0;
    else return 1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.