aspose file tools
The moose likes Java in General and the fly likes How to sort an ArrayList of ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to sort an ArrayList of ArrayLists" Watch "How to sort an ArrayList of ArrayLists" New topic
Author

How to sort an ArrayList of ArrayLists

vrayudu kumar
Ranch Hand

Joined: Nov 20, 2003
Posts: 77
Hello All,

How to sort an ArrayList which contains array of ArrayLists
ArrayList = [
["-1","qaaa","test"],
["-1","qaaa","test123"],
["-1","bbba","test123"],
["-1","qaaa","test123"],
["-1","qaaa","test123"],
["-1","bbba","test123"]]

i want to sort this arraylist based on the second parameter in each arraylist.

How can i solve this.

Thanks in advance.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24050
    
  13

Use the static sort() method in the java.util.Collections class which takes a List and a Comparator as arguments. You have to write a class which implements Comparator, and in the compare() method, you'll can expect two Lists as arguments. Extract the appropriate entry from each of them, and compare the entries, returning the comparison value. It might look like (this is JDK 1.5, which simplifies things quite a bit



[Jess in Action][AskingGoodQuestions]
Gowri Kalyani K V
Greenhorn

Joined: Jul 22, 2005
Posts: 1
what's the way to implement the same in jdk1.4
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
You've just to do explicit cast (from list.get(1) to String)


Regards,<br />Seb<br /> <br />SCJP 1.4
vrayudu kumar
Ranch Hand

Joined: Nov 20, 2003
Posts: 77
I did the casting to String, Its working fine. Thanks seb
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to sort an ArrayList of ArrayLists
 
Similar Threads
Multidimensional Array List (Array List of array list)
Comparing two ArrayList
Matrices in Java
How can I manually add values to ArrayList[]
Java constructor