cancel
Showing results for 
Search instead for 
Did you mean: 

how to sort an arraylist

srinivas_sistu
Active Contributor
0 Kudos

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)

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

See thread.

Regards,

Satyajit.

Former Member
0 Kudos

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_sistu
Active Contributor
0 Kudos

hi Satyajit.

actually, in the link given by u they are comparing array having only one field...

srinivas_sistu
Active Contributor
0 Kudos

what is this "MdmRecordBean) o1"?

srinivas_sistu
Active Contributor
0 Kudos

i am using dynamic UIs here.. So when i am placing this code given by u it is saying only abstract or final is allowed(inside wddomodify())

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,

How are you adding these data to the ArrayList? Are they objects of some class like for example "MemberData"? Do you finally want to sort the context node elements?

Regards,

Satyajit.

Answers (0)