Roughly coded a customized Sorting example for my colleague
Test result:
/*
the class wants to be sorted.
*/
global Class commonClass implements Comparable {
public Decimal HNPVI{get;set;}
public Decimal PVI{get;set;}
public Decimal PSV{get;set;}
public static String sortBy{get; set;}
public commonClass(Decimal dec1, Decimal dec2, Decimal dec3){
this.HNPVI = dec1;
this.PVI = dec2;
this.PSV = dec3;
}
// Implement the compareTo() method
global Integer compareTo(Object compareTo) {
commonClass compareToObj = (commonClass )compareTo;
if(commonClass.SortBy == 'PSV'){
if (PSV == compareToObj.PSV) return 0;
if (PSV > compareToObj.PSV) return 1;
return -1;
}else if(commonClass.SortBy == 'PVI'){
if (PVI== compareToObj.PVI) return 0;
if (PVI> compareToObj.PVI) return 1;
return -1;
}else if(commonClass.SortBy == 'HNPVI'){
if (HNPVI== compareToObj.HNPVI) return 0;
if (HNPVI> compareToObj.HNPVI) return 1;
return -1;
}else
return -1;
}
}
here is the test class
@isTest
public class SortingTest {
static testmethod void test1() {
List<commonClass> ccList = new List<commonClass>();
commonClass.sortBy = 'PVI';
commonClass cc1 = new commonClass(101.3,103.2,103.2);
commonClass cc2 = new commonClass(25.6,102.2,102.2);
commonClass cc3 = new commonClass(105.8,101.2,101.2);
ccList.add(cc1);
ccList.add(cc2);
ccList.add(cc3);
ccList.sort();
System.debug( ' %%PVI ' + ccList);
commonClass.sortBy = 'HNPVI';
ccList.sort();
System.debug( ' %%HNPVI ' + ccList);
commonClass.sortBy = 'PSV';
ccList.sort();
System.debug( ' %%PSV ' + ccList);
}
}
Test result:
15:51:51:044 USER_DEBUG [15]|DEBUG| %%PVI (
commonClass:[HNPVI=105.8, PSV=101.2, PVI=101.2, sortBy=PVI],
commonClass:[HNPVI=25.6, PSV=102.2, PVI=102.2, sortBy=PVI],
commonClass:[HNPVI=101.3, PSV=103.2, PVI=103.2, sortBy=PVI])
15:51:51:045 USER_DEBUG [19]|DEBUG| %%HNPVI (
commonClass:[HNPVI=25.6, PSV=102.2, PVI=102.2, sortBy=HNPVI],
commonClass:[HNPVI=101.3, PSV=103.2, PVI=103.2, sortBy=HNPVI],
commonClass:[HNPVI=105.8, PSV=101.2, PVI=101.2, sortBy=HNPVI])
15:51:51:046 USER_DEBUG [23]|DEBUG| %%PSV (
commonClass:[HNPVI=105.8, PSV=101.2, PVI=101.2, sortBy=PSV],
commonClass:[HNPVI=25.6, PSV=102.2, PVI=102.2, sortBy=PSV],
commonClass:[HNPVI=101.3, PSV=103.2, PVI=103.2, sortBy=PSV])