• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to sort an ArrayList of ArrayLists

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's the way to implement the same in jdk1.4
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've just to do explicit cast (from list.get(1) to String)
 
vrayudu kumar
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did the casting to String, Its working fine. Thanks seb
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic