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.

vikas27
  • 563
  • 5
  • 14
  • 36
  • 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? – Anders R. Bystrup Oct 31 '12 at 08:46
  • How will the ArrayList be sorted? You need to provide comparator for that too. – Subir Kumar Sao Oct 31 '12 at 08: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`? If second, what criteria you want to use? – brimborium Oct 31 '12 at 08:47
  • I need to sort the outer one on the basis of the bean date value. – vikas27 Oct 31 '12 at 08:51
  • you need to write your custom `comparator` and use it with the `Collection.sort(list,newcomparator)` method – Mukul Goel Oct 31 '12 at 08:52
  • That does not make sense unless all beans in the inner list have the same date or you spec an algorithm to determine how a given set of dates are "higher"/"lower" than another set of dates! – Anders R. Bystrup Oct 31 '12 at 08:53

4 Answers4

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?

Aubin
  • 14,617
  • 9
  • 61
  • 84
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;
André Stannek
  • 7,773
  • 31
  • 52
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,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
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;
}
Kishor Sharma
  • 599
  • 8
  • 15