Forums Register Login

Sorting of List?

+Pie Number of slices to send: Send
Hi friends,

In my program I have a list of beans.. For example,
myBean contains 4 elemets like r1,r2,r3 and r4, myList contains list of myBean objects. How can I sort the List based on any one of value(Eg:r1) in the bean object..

please help me out..

Thanks in advance..
+Pie Number of slices to send: Send
Have you looked at the sort methods in java.util.Collections? Providing your own Comparator should allow you to sort as desired.
+Pie Number of slices to send: Send
Hi..

I know that method sort(List l, Comparator c)..

But I dont know how to use it in my case..

Could you please give me some sample code..
+Pie Number of slices to send: Send
Here's a quick example...
+Pie Number of slices to send: Send
Hi marc,

Thanks a Lot..

It's working fine..
+Pie Number of slices to send: Send
Hi..

I have one more doubt..
In your example all of the attributes datatype is int..
In my example all of them are String.. But based on their original datatype(which I will get from DataBase) I have to sort them..
How can I do the sorting for different datatypes..

Can you help me..
[ March 12, 2007: Message edited by: Jessica James ]
+Pie Number of slices to send: Send
Luckily, java.lang.String has a compareTo() method that you can use in your compare method. Be careful, though: with Strings you have to allow for the fact that the String may be null.

If you have custom objects you'd like to sort, have them extend the java.lang.Comparable interface. You will then have to provide your own compareTo() method for the object. Incidentally, if you have a List of objects that implement Comparable, you can sort them without specifying a Comparator.
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1030 times.
Similar Threads
itrating through an arraylist
[Struts2] dinamic name for textfield
Servlet equivalent to <jsp:useBean ../>
Problem adding in ArrayList
Cating Object to ComplexType
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:01:07.