Last active
April 2, 2018 14:20
-
-
Save Sunil02kumar/1db37566dbc47a2b29a871d107e5073f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SK_sortUtilityTestWrapper { | |
public string category{get;set;} | |
public decimal count{get;set;} | |
public SK_sortUtilityTestWrapper(string ss,decimal sk){ | |
this.category=ss; | |
this.count=sk; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SK_WrapperSortUtility { | |
//We will sort wrapper list based on count value | |
//using bubble sort | |
public static List<SK_sortUtilityTestWrapper> sortWrapperList(List<SK_sortUtilityTestWrapper> recList,string sortType){ | |
List<SK_sortUtilityTestWrapper> sortedList = new List<SK_sortUtilityTestWrapper>(); | |
system.debug('******sortedList before sorting in '+sortType+':'+recList); | |
if(recList.size()>0){ | |
sortedList = recList; | |
if(sortType.equalsIgnoreCase('DESC')){ | |
for(integer i = 0; i < sortedList.size(); i++){ | |
for(integer j = 1; j < (sortedList.size()-i); j++){ | |
Decimal firstValue = decimal.valueof(string.valueof(sortedList[j-1].count)); | |
Decimal nextValue = decimal.valueof(string.valueof(sortedList[j].count)); | |
//if firstValue < nextValue, swap the elements | |
if(firstValue < nextValue){ | |
SK_sortUtilityTestWrapper tmpValue = sortedList[j-1]; | |
sortedList[j-1]=sortedList[j]; | |
sortedList[j]=tmpValue; | |
} | |
} | |
} | |
}else if(sortType.equalsIgnoreCase('ASC')){ | |
for(integer i = 0; i < sortedList.size()-1; i++){ | |
for(integer j = 1; j < sortedList.size(); j++){ | |
Decimal firstValue = decimal.valueof(string.valueof(sortedList[j-1].count)); | |
Decimal nextValue = decimal.valueof(string.valueof(sortedList[j].count)); | |
//if firstValue > nextValue, swap the elements | |
if(firstValue > nextValue){ | |
SK_sortUtilityTestWrapper tmpValue = sortedList[j-1]; | |
sortedList[j-1]=sortedList[j]; | |
sortedList[j]=tmpValue; | |
} | |
} | |
} | |
} | |
} | |
system.debug('******sortedList after sorting in '+sortType+':'+sortedList); | |
return sortedList; | |
} | |
public static void testWrapperListSorting(){ | |
List<SK_sortUtilityTestWrapper> wrapperList= new List<SK_sortUtilityTestWrapper>(); | |
wrapperList.add(new SK_sortUtilityTestWrapper('D',50.6)); | |
wrapperList.add(new SK_sortUtilityTestWrapper('A',40)); | |
wrapperList.add(new SK_sortUtilityTestWrapper('A1',40)); | |
wrapperList.add(new SK_sortUtilityTestWrapper('B',44.2)); | |
wrapperList.add(new SK_sortUtilityTestWrapper('C',49)); | |
List<SK_sortUtilityTestWrapper> sortedWrapperASC=SK_WrapperSortUtility.sortWrapperList(wrapperList,'ASC'); | |
List<SK_sortUtilityTestWrapper> sortedWrapperDESC=SK_WrapperSortUtility.sortWrapperList(wrapperList,'DESC'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment