How to Sort Wrapper class Collection in Apex - Salesforce ...

文章推薦指數: 80 %
投票人數:10人

Sorting of Primitive Data Types like (eg. List of student names) is easy in collection. But suppose you have a scenario like below,. Thursday,May4,2017 HowtoSortWrapperclassCollectioninApex SortingofPrimitiveDataTypeslike(eg.Listofstudentnames)iseasyincollection. Butsupposeyouhaveascenariolikebelow, Youhavealistofemployees(WrapperClass). SaythisEmployeewrappercontainsfewprimitivetypelikecompareToEmpl.emplInst.empName){ //Setreturnvaluetoapositivevalue. returnValue=1; }elseif(emplInst.empNamegetListOfEmpNameAsSortedOrder(){ ListlstOfEmps=newList(); lstOfEmps=getListOfEmployee(); System.debug('Simplelistofemployee:::::::::::::::'+lstOfEmps); ListlstOfSortedEmps=newList(); for(EmployeeWrapperEgempIns:lstOfEmps){ lstOfSortedEmps.add(newEmployeeComparatorEg(empIns)); } lstOfSortedEmps.sort(); System.debug('Sortedlistbyname:::::::::::::::'+lstOfSortedEmps); returnlstOfSortedEmps; } /**Initializelistofemployeewrapper.ForsimplicityIhavemakeitmanually. *Itcanbesetfromdifferentsourcelikedatabaseoranyexternalsystem. */ privatestaticListgetListOfEmployee(){ ListlstOfEmps=newList(); EmployeeWrapperEgepm1=newEmployeeWrapperEg(); epm1.empName='Mark'; epm1.empId='E-100'; epm1.empSalary=10000; EmployeeWrapperEgepm2=newEmployeeWrapperEg(); epm2.empName='Jon'; epm2.empId='E-102'; epm2.empSalary=9000; EmployeeWrapperEgepm3=newEmployeeWrapperEg(); epm3.empName='Luce'; epm3.empId='E-101'; epm3.empSalary=9500; lstOfEmps.add(epm1); lstOfEmps.add(epm2); lstOfEmps.add(epm3); returnlstOfEmps; } } ###########FromAnonymousWindowcall getListOfEmpNameAsSortedOrder()methodofabove EmployeeComparatorTestEgclasstocheck thenormalandsortedlistofemployeeindebuglog.########### ExecutethebelowlineofcodeinAnonymouswindow. EmployeeComparatorTestEg.getListOfEmpNameAsSortedOrder(); Output: 15:32:48:013USER_DEBUG[10]|DEBUG|Simplelistofemployee::::::::::::::: (EmployeeWrapperEg:[empId=E-100,empName=Mark,empSalary=10000.0],  EmployeeWrapperEg:[empId=E-102,empName=Jon,empSalary=9000.0],  EmployeeWrapperEg:[empId=E-101,empName=Luce,empSalary=9500.0]) 15:32:48:014USER_DEBUG[18]|DEBUG|Sortedlistbyname::::::::::::::: (EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-102,empName=Jon,empSalary=9000.0]],  EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-101,empName=Luce,empSalary=9500.0]],  EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-100,empName=Mark,empSalary=10000.0]]) at May04,2017 EmailThisBlogThis!SharetoTwitterSharetoFacebookSharetoPinterest Nocomments: PostaComment NewerPost Home Subscribeto: PostComments(Atom) LWCexample:WaytocallApexImperatively. Example:CreateaLWCcomponentwhichwillcallApexImperativelywithparameters.Letstakeanexamplelikedisplaymax5Accountrecords... Cratealightningcomponenttoinsertrecord. Cratealightningcomponenttoupdaterecord. ArunKumarHazra SalesforceCertifiedApplicationArchitect Viewmycompleteprofile BlogArchive ►  2020 (7) ►  October2020 (7) ►  2019 (30) ►  September2019 (9) ►  June2019 (9) ►  May2019 (8) ►  April2019 (1) ►  February2019 (1) ►  January2019 (2) ►  2018 (9) ►  November2018 (2) ►  May2018 (4) ►  February2018 (2) ►  January2018 (1) ▼  2017 (23) ►  December2017 (7) ►  July2017 (1) ►  June2017 (5) ▼  May2017 (10) SampleexampleforconsumingRestWebService(Res... RestWebServiceApexCodeSample(WebMethodcont... SampleexampleforconsumingRestWebService(Res... SampleexampleforconsumingRestWebService(Res... RestWebServiceApexCodeSample. HowtocreateajarfileforWSDLgeneratedfromS... HowtocallSalesforceSOAPservicefromjava. HowtocallWebServicesMethod(SOAP)fromoneSa... SOAPWebServiceExampleinApex. HowtoSortWrapperclassCollectioninApex Followers



請為這篇文章評分?