This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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..
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 ]
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.