0

I want to sort an ArrayList of ArrayList which are having objects inside it. Sorting needs to be done based on some attributes of Objects inside the ArrayList. It is like

ArrayList<ArrayList<Object>> list=new ArrayList<ArrayList<Object>>();

I want to use Collections's Sort method.

I have tried following but it is displaying error

Collections.sort(ccWrapper.colContainer, new Comparator<ArrayList<Object>>() {
public int compare(Object arg0, Object arg1) {

            // TODO Auto-generated method stub

return 0;
}});

Error:The type Comparator is not generic; it cannot be parameterized with arguments >

WitVault
  • 23,445
  • 19
  • 103
  • 133

3 Answers3

1

Use Comparator or Comaparable interfaces with your objects

http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
Deepak
  • 2,287
  • 1
  • 23
  • 30
  • 2
    Wouldn't it be cool if there was some kind of [ComparableComparator](http://www.frischcode.com/2013/11/help-i-have-comparable-but-i-need.html)? – Elliott Frisch Dec 12 '13 at 05:47
1

I would suggest you define your own custom List that delegates to an ArrayList and also happens to be Comparable:

public class SortableList implements List<Object>, Comparable<SortableList> {
    private ArrayList delegate;

    public SortableList(List list) {
        delegate = new ArrayList(list);
    }

    @Override
    public int compareTo(SortableList s) {
        //Do what you need to do with your casts and comparisons
        return 0;
    }

    //And implement all the other methods in List by simply delegating to the ArrayList member variable
}

You could also do something similar and less tedious by extending ArrayList, but I have a real issue working off a concrete class. Better in my opinion to base your class on higher-level abstractions. But I digress.

Now that Comparable is implemented, you can do this:

List<SortableList> bigList = new ArrayList<>();
//Do stuff with bigList
Collections.sort(bigList);

You don't need Comparator since Comparable will provide the natural ordering for your custom list. You don't appear to need another means of comparison.

If that isn't clear, we actually did a tutorial on Comparable and Comparator with the files available on Github. Check it out if you like.

Vidya
  • 29,932
  • 7
  • 42
  • 70
0

Implement the compareTo method for the "Object" class that you are storing inside the array lists then use the built-in Collections.sort() on the inner arraylist for each of the outer arraylists.

Zeb Barnett
  • 168
  • 2
  • 11
  • "Sorting needs to be done based on some attributes of Objects inside the ArrayList" . The default Collections.sort wont help.. He will have to write code to sort both inner and outer arraylists. – TheLostMind Dec 12 '13 at 05:48
  • Oh, it seemed to me that the lists as entities themselves weren't being sorted but the objects in each list. My bad :( – Zeb Barnett Dec 12 '13 at 05:49
  • nevermind... Please be careful the next time around... :) – TheLostMind Dec 12 '13 at 05:52