A very interesting requirement i came through today ie to sort a wrapper record list. While exploring i got to know that apex provides an ...
Skiptocontent
September28,2020AlokkumarMishra
Averyinterestingrequirementicamethroughtodayietosortawrapperrecordlist.WhileexploringigottoknowthatapexprovidesaninterfacecalledcomparablewithmethodnamedcompareTo.Thisinterfacedothemagicofsortinginwrapperclass.Belowisthesamplecodeforthesame.
publicclassouterClass{
publicvoidsortingCheck(){
listwrapperRecords=newlist();
innerClasswr1=newinnerClass('Alok','Kumar');
innerClasswr2=newinnerClass('TestAlok','Kumar');
innerClasswr3=newinnerClass('Kumar','Alok');
wrapperRecords.add(wr1);
wrapperRecords.add(wr2);
wrapperRecords.add(wr3);
wrapperRecords.sort();
}
publicclassinnerClassimplementsComparable{
publicstringfirstName{get;set;}
publicstringlastName{get;set;}
publicinnerClass(StringfirstN,StringlastN){
firstName=firstN;
lastName=lastN;
}
publicintegercompareTo(ObjectrecordTocompare){
returnfirstName.compareTo(((innerClass)recordTocompare).firstName);
}
}
}
InAbovecodetheinnerclassimplementscomparableinterface.
CompareTomethodsfollowsbelowrule:-
0ifthisinstanceand recordtocompareareequal>0ifthisinstanceisgreaterthan recordtocompare<0ifthisinstanceislessthan recordtocompare
theabovecodeinturnreturnthelistsortedbasedonfirstname.
Sharethis:TwitterFacebookLinkedInPinterestWhatsAppSkypeLikethis:LikeLoading...
Related
PublishedbyAlokkumarMishra
ViewallpostsbyAlokkumarMishra
Postnavigation
PreviouspostPassRecordInFlowFromApp BuilderNextpostHavingKeywordIn SOQL
LeaveaReplyCancelreply
Enteryourcommenthere...
Fillinyourdetailsbeloworclickanicontologin:
Email(Addressnevermadepublic)
Name
Website
YouarecommentingusingyourWordPress.comaccount.
( Log Out /
Change )
YouarecommentingusingyourGoogleaccount.
( Log Out /
Change )
YouarecommentingusingyourTwitteraccount.
( Log Out /
Change )
YouarecommentingusingyourFacebookaccount.
( Log Out /
Change )
Cancel
Connectingto%s
Notifymeofnewcommentsviaemail.Notifymeofnewpostsviaemail.
Δ
Searchfor:
BlogStats
53,065hits
FollowUs
Twitter
LinkedIn
Categories
Jquery(1)
LightningFlows(6)
LightningSFDC(27)
LightningWebComponent(4)
OpenID(1)
Servicecloud(2)
SFDCconfiguration(9)
SFDCCustomization(17)
Uncategorized(8)
FollowSFDCKnowledgeArticlesonWordPress.com
RecentComments
RevathyonJWTTokenIn ApexAlokkumarMishraonDependentPicklistin LWCKevinNelsononDependentPicklistin LWCAnonymousonCustomHeaderandFooterinLi…AlokkumarMishraonCustomHeaderandFooterinLi…
CreateyourwebsitewithWordPress.comGetstarted
Privacy&Cookies:Thissiteusescookies.Bycontinuingtousethiswebsite,youagreetotheiruse.
Tofindoutmore,includinghowtocontrolcookies,seehere:
CookiePolicy
Follow
Following
SFDCKnowledgeArticles
Join640otherfollowers
Signmeup
AlreadyhaveaWordPress.comaccount?Loginnow.
SFDCKnowledgeArticles
Customize
Follow
Following
Signup
Login
Copyshortlink
Reportthiscontent
ViewpostinReader
Managesubscriptions
Collapsethisbar
%dbloggerslikethis: