Use Collections.sort(List list, Comparator c)
Just create a comparator for the fields you want to sort on. Look at
the api doc for the Comparator class. The other way to do it would be
to have your class implement Comparable. That would define it's
"natural ordering" or sort order without using a Comparator.
Comparators are more usefull if you ever may need to sort by more than
one field or if there is no distict field which should constitue the
natural ordering of the objects in the List.
Here is a sample Comparator:
class Subject_MessageComparator implements Comparator
{
public int compare(Object messageWrapper1, Object
messageWrapper2)
{
try
{
String messageSubject1 =
((MessageWrapper)messageWrapper1).getSubject();
String messageSubject2 =
((MessageWrapper)messageWrapper2).getSubject();
if (messageSubject1 == null)
return -1;
else
return messageSubject1.compareTo(messageSubject2);
}
catch (Exception e)
{
return 0;
}
}
}
-Eric Dalquist
ho**@cryptomail.org (Huzefa) wrote in message news:<85*************************@posting.google.c om>...
Hi,
My question is that when you place objects in an ArrayList, is there
any library function that can sort them based on different fields of
the object. I tried using
Containers.sort(myList);
where myList is the name of my list.
Thanx,
Huzefa