on 02-01-2007 1:09 PM
hi all, I am having an arraylist which is holding data of a membercountry of an organisation... the data is like Formalname, Date of MemberShip, No of Shares.... etc... now i want to sort the arraylist based on the value of FormalName(in ascending or Descending order)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi srinivas,
if "records" is the name of your list, and it contains beans with data you want (Formalname, Date of MemberShip, No of Shares)
.............
Collections.sort(records, new listComparator());
.............
public class listComparator implements java.util.Comparator {
public int compare ( Object o1, Object o2 ) {
String acc1 = "";
String acc2 = "";
acc1 = ((MdmRecordBean) o1).getFormalname();
acc2 = ((MdmRecordBean) o2).getFormalname();
return acc1.compareTo(acc2);
}
}
Hope this help you,
Vito
Srinivas this is an example,
in this case MDMRecordbean is the type of the bean in the list.
You can create a bean with your attribute (Formalname, Date of MemberShip, No of Shares.... etc...). You have to create a method where you put in the listthe beans of type that you want. (Instead of a bean you can you others object).
When you call listComparator, you need to cast the Object (O1 and O2) to the type of bean.... basic in java, in fact Object is general.
Bye,
Vito
User | Count |
---|---|
86 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.