Recently, I had a requirement to sort more than one field/column and display the result in visualforce page.
The Sorting order should be in the following way,
FIRST COLUMN should be in DESCENDING order.
SECOND COLUMN should be in ASCENDING order.
Wrapper Class:
- public class PurchaseHistoryWrapper implements Comparable
- {
- public enum SortField{purchasedYearEnum, purchasedQtyEnum}
- public static SortField SORT_FIELD = SortField.purchasedYearEnum;
- public String purchasedYear{get;set;}
- public String purchasedQty{get;set;}
- public Integer purchasedYearInt{
- get{
- if(purchasedYear != null){
- return Integer.valueOf(purchasedYear);
- }return 0;
- }set;
- }
- public Integer purchasedQtyInt{
- get{
- if(purchasedQty != null){
- return Integer.valueOf(purchasedQty);
- }return 0;
- }set;
- }
- public Integer compareTo(Object other) {
- if(SORT_FIELD == SortField.purchasedYearEnum)
- return compareToPurchasedYear(other);
- else if (SORT_FIELD == SortField.purchasedQtyEnum)
- return compareToPurchasedQty(other);
- else
- return 0;
- }
- public Integer compareToPurchasedYear(Object compareTo){
- PurchaseHistoryWrapper compareToModel = (PurchaseHistoryWrapper)compareTo;
- // Decending Order comparison.
- if(purchasedYearInt < compareToModel.purchasedYearInt)
- return 1;
- else if(purchasedYearInt == compareToModel.purchasedYearInt)
- return 0;
- else
- return -1;
- }
- public Integer compareToPurchasedQty(Object compareTo){
- PurchaseHistoryWrapper compareToModel = (PurchaseHistoryWrapper)compareTo;
- // Ascending Order comparison.
- if(purchasedQtyInt > compareToModel.purchasedQtyInt)
- return 1;
- else if(purchasedQtyInt == compareToModel.purchasedQtyInt)
- return 0;
- else
- return -1;
- }
- }
Apex Class:
- Public class PurchaseHistoryController
- {
- public List<PurchaseHistoryWrapper> purchaseList{get;set;}
- public string puchaseListString;
- public PurchaseHistoryController()
- {
- String jsonString = '[ { "purchasedYear": "2014", "purchasedQty":"25" }, { "purchasedYear": "2015", "purchasedQty":"2" }, { "purchasedYear": "2014", "purchasedQty":"1" }, { "purchasedYear": "2015", "purchasedQty":"12" }, { "purchasedYear": "2014", "purchasedQty":"222" }, { "purchasedYear": "2015", "purchasedQty":"3" } ]';
- purchaseList = (List<PurchaseHistoryWrapper>) System.JSON.deserialize(jsonString, List<PurchaseHistoryWrapper>.class);
- if(purchaseList != null && purchaseList.size() > 0)
- {
- purchaseList.sort();
- puchaseListString = multiSort(purchaseList);
- purchaseList = (List<PurchaseHistoryWrapper>) System.JSON.deserialize(puchaseListString, List<PurchaseHistoryWrapper>.class);
- }
- }
- public String multiSort(List<PurchaseHistoryWrapper> purchaseList)
- {
- List<PurchaseHistoryWrapper> tempPurchaseList = new List<PurchaseHistoryWrapper>();
- Map<String,List<PurchaseHistoryWrapper>> phMap = new Map<String,List<PurchaseHistoryWrapper>>();
- for(PurchaseHistoryWrapper currPurchase: purchaseList)
- {
- if(phMap.containsKey(currPurchase.purchasedYear))
- {
- phMap.get(currPurchase.purchasedYear).add(currPurchase);
- }
- else
- {
- phMap.put(currPurchase.purchasedYear,new List<PurchaseHistoryWrapper>{currPurchase});
- }
- }
- for(String key : phMap.keySet())
- {
- List<PurchaseHistoryWrapper> phTemp = phMap.get(key);
- //Enum
- PurchaseHistoryWrapper.SORT_FIELD = PurchaseHistoryWrapper.SortField.purchasedQtyEnum;
- phTemp.sort();
- tempPurchaseList.addAll(phTemp);
- }
- return json.serialize(tempPurchaseList);
- }
- }
Visualforce Page:
- <apex:page controller="PurchaseHistoryController">
- <apex:form >
- <apex:pageBlock >
- <apex:pageBlockTable value="{!purchaseList}" var="pl">
- <apex:column headerValue="Purchased Year" value="{!pl.purchasedYear}"/>
- <apex:column headerValue="Number of Quantity" value="{!pl.purchasedQty}"/>
- </apex:pageBlockTable>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
Output Page:
No comments:
Post a Comment