Comparable Interface in Salesforce - InfallibleTechie

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

To add List sorting support for your Apex class, you must implement the Comparable interface with its compareTo method in your class. PagesHomeAboutMeContactUsComparableInterfaceinSalesforceTheComparableinterfaceaddssortingsupportforListsthatcontainnon-primitivetypes,thatis,Listsofuser-definedtypes.ToaddListsortingsupportforyourApexclass,youmustimplementtheComparableinterfacewithitscompareTomethodinyourclass.WhenaclassimplementstheComparableinterfaceithastoimplementamethodcalledcompareTo().Inthatmethodthecodehastobewrittentodecideiftwoobjectsmatchorifoneislargerthantheother.Formoreinfo,visitthebelowlinkhttp://www.salesforce.com/us/developer/docs/apexcode/Content/apex_comparable.htmSampleCode:Visualforcepage:

                        
   ApexController:globalclassSample{     publicListStudList{get;set;}    publicSample(){    StudList=newList();    Students1=newStudent('Ganesh',12);    StudList.add(s1);         Students2=newStudent('Arun',82);    StudList.add(s2);         Students3=newStudent('Vignesh',32);    StudList.add(s3);         Students4=newStudent('Younis',55);    StudList.add(s4);            Students5=newStudent('Azhar',42);    StudList.add(s3);           StudList.sort();  }     //wrapperclassforComparableInterface     globalclassStudentimplementsComparable{    globalStringStudentName{get;set;}      globalIntegerStudentAge{get;set;}         globalStudent(StringName,IntegerAge){      StudentName=Name;      StudentAge=Age;    }         globalIntegercompareTo(ObjectObjToCompare){      returnStudentName.CompareTo(((Student)ObjToCompare).StudentName);    }  }}Output:Labels: SalesforceVisualforcePage, Salesforce.com1comment:AnonymousWednesday,September02,2015Thanksforthepost.Itworks.ButifIwanttosortby2columns.LikeAftersortingbyStudentName,IneedtosortbystudentAge.(insoqlterms,ORDERBYNameASC,AgeASC).Howtodo?Pleaseletmeknow.Thankyou.ReplyDeleteRepliesReplyAddcommentLoadmore... NewerPost OlderPost HomeSubscribeto: PostComments(Atom)TopLabelsSalesforceChatSalesforceInterviewQuestionsSalesforceLightningWebComponentSalesforceMFAreportthisadxx



請為這篇文章評分?