• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting an arraylist of arrays

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an ArrayList, which contains X amount of String[]s. Each string array has stuff like Name, Surname, Address, Town, etc.

I want to be able to sort the ArrayList of arrays, by a specified entry in each array. name, or surname, or... etc. I assume there would be a sort method, which would take a parameter searchBy or something like that.

What would be the best way to do this?

Thanks in advance for any help.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you can guarentee the ordering of you String arrays? So you know that surname is always element 2 for example?

What you could do is write a Comparator which has an extra property which represents the element in your String array you want to use to sort your List by. Then you could use Collections.sort(List list, Comparator c).
[ April 26, 2005: Message edited by: Paul Sturrock ]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stuart,

You might try to:
1- Write your own Comparator (that compares two entries in your ArrayList)
2- Use Collections.sort(myArraylist, myComparator)

You may find details on Comparator
here

Best regards,
reply
    Bookmark Topic Watch Topic
  • New Topic