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.

[462 byte] By [Sarayu_Geethajalia] at [2007-11-26 12:26:44]
# 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

prometheuzza at 2007-7-7 15:33:07 > top of Java-index,Other Topics,Algorithms...
# 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.

Sarayu_Geethajalia at 2007-7-7 15:33:07 > top of Java-index,Other Topics,Algorithms...
# 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.

prometheuzza at 2007-7-7 15:33:07 > top of Java-index,Other Topics,Algorithms...
# 4
Thanks alot.It's working fine.Regards,Sarayu
Sarayu_Geethajalia at 2007-7-7 15:33:07 > top of Java-index,Other Topics,Algorithms...
# 5
> Thanks alot.> > It's working fine.> > Regards,> SarayuGood to hear it!You're welcome.
prometheuzza at 2007-7-7 15:33:07 > top of Java-index,Other Topics,Algorithms...