Need some help how to sort the bean data
Hi,
I need to sort the values from the Bean.
For Eg:
List list;
list = Sample.getList(); // returns list of bean objects.
for (int i= 0; i < list.size(); i++) {
TestBean bean = (TestBean)list.get(i);
name = bean.getDisplayMibName();
}
I need to sort the names from the bean and assign to new list.
New list contains bean objects in sorted order.(alphabetical order).
Thanks in Advance.
# 1
> I need to sort the names from the bean and assign to
> new list.
> New list contains bean objects in sorted
> order.(alphabetical order).
Let your TestBean implement the Comparable interface and then call java.util.Collections.sort(list) to sort the list.
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
# 2
Hi,
Thanks for the reply,
My application must use jdk1.4.
Comparable<T> supports only 5.0.
My actually requirement is
displaying a dialog with four radio buttons (Name, submitter, reviewer and lastmodifieddate).
If user selects the radio button of submitter, the bean values should be sorted based on submitter names.
List list = getAllValues(); // returns bean objects
If I use Collections.sort(list);
It sorts only names. But i need to sort based on user input.
Please let me know how to handle this scenario.
Thanks,
Sarayu.
# 3
> Hi,
>
> Thanks for the reply,
> My application must use jdk1.4.
> Comparable<T> supports only 5.0.
Then remove all the <>'s and all text between them. It should work that way for JDK's <= 1.4.
> ...
> If user selects the radio button of submitter, the
> bean values should be sorted based on submitter
> names.
> List list = getAllValues(); // returns bean objects
> If I use Collections.sort(list);
> It sorts only names. But i need to sort based on user
> input.
You can write different Comparator's for that.
See the link I posted earlier.