public class WrapperClass implements Comparable {
public Contact idm;
public String name;
public String phone;
// Constructor
public WrapperClass (Contact i, String n, String p) {
idm = i;
name = n;
phone = p;
}
// Implement the compareTo() method
public Integer compareTo(Object compareTo) {
WrapperClass compareToWarp = (WrapperClass)compareTo;
if (idm.lastname == compareToWarp.idm.lastname) return 0;
if (idm.lastname > compareToWarp.idm.lastname) return 1;
return -1;
}
}
-----------------------------
public class WrapperSorting
{
public WrapperSorting()
{
List<WrapperClass> wrapList=new List<WrapperClass>();
wrapList.add(new WrapperClass(new Contact(lastname='jo jo'),'Joe Smith', '4155551212'));
wrapList.add(new WrapperClass(new Contact(lastname='Zo jo'),'J. Smith', '4155551212'));
wrapList.add(new WrapperClass(new Contact(lastname='Ko jo'),'Caragh Smith', '4155551000'));
wrapList.add(new WrapperClass(new Contact(lastname='Po jo'),'Mario Ruiz', '4155551099'));
wrapList.sort();
for(WrapperClass obj:wrapList)
{
System.debug('Test='+obj);
}
}
}
No comments:
Post a Comment