Sort an ArrayList?

J

Jaba

Is there a clean way to sort an ArrayList loaded with Objects of a
class based on a variable within that object?
 
R

Ryan Stewart

Jaba said:
Is there a clean way to sort an ArrayList loaded with Objects of a
class based on a variable within that object?
See java.lang.Comparable, java.util.Comparator, and java.util.Collections.
Collections.sort(List) takes a list of objects that implement the Comparable
interface. Collections.sort(List, Comparator) takes a list of anything and a
Comparator that knows how to compare them.
 
O

Oscar kind

Ryan Stewart said:
See java.lang.Comparable, java.util.Comparator, and java.util.Collections.
Collections.sort(List) takes a list of objects that implement the Comparable
interface. Collections.sort(List, Comparator) takes a list of anything and a
Comparator that knows how to compare them.

Also, the BeanComparator class in the jakarta-commons beanutils library
may come in handy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top