Wrapper Class - Sort List by Date

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

use list.sort(); to sort your dates. Since, this sorts in ascending direction automatically, iterate through this from the last record. for (i = ... LoginwithSalesforce Don'thaveanaccount? SignupforaDeveloperEdition BrowsebyTopic ApexCodeDevelopment(86622) GeneralDevelopment(53776) VisualforceDevelopment(36733) APIsandIntegration(15806) Lightning(15647) Trailhead(11258) Formulas&ValidationRulesDiscussion(10640) OtherSalesforceApplications(7731) JobsBoard(6626) Force.comSites&Site.com(4714) Mobile(2608) JavaDevelopment(3888) .NETDevelopment(3500) Security(3170) Mobile(2608) VisualWorkflow(2338) AppExchangeDirectory&Packaging(2325) Perl,PHP,Python&RubyDevelopment(2012) ChatterandChatterAPIDevelopment(1702) SalesforceLabs&OpenSourceProjects(1215) DesktopIntegration(1140) Architecture(926) SchemaDevelopment(912) Apple,MacandOSX(791) VBandOfficeDevelopment(633) EinsteinPlatform(188) Salesforce$1MillionHackathon(180) SalesforceSummerofHacks(173) ViewMoreTopics SeeAllPosts Ask Search: ResetSearch WelcometoSupport! SearchforanansweroraskaquestionofthezoneorCustomerSupport. Needhelp? Dismiss Youneedtosignintodothat Signintostartsearchingquestions Don'thaveanaccount? SignupforaDeveloperEdition ShowAllQuestionssortedbyDatePosted ShowAllQuestionsUnansweredQuestionsUnsolvedQuestionsSolvedQuestions sortedbyDatePostedRecentActivityMostPopular +StartaDiscussion Youneedtosignintodothat Signintostartadiscussion Don'thaveanaccount? SignupforaDeveloperEdition SolvedQuestionsThisQuestion RepsG WrapperClass-SortListbyDateHiGuys, IwaswonderingifanybodycanhelpmesortawrapperclassbyDate. Ihavedatafromthreeobjectgoingintoalistvariable.Howdoithensortthelistbydate(decending)? publicclasshistory{    publicString title   {get;set;}    publicString Description  {get;set;}    publicString primaryAccount  {get;set;}    publicdatetimecreatedDated  {get;set;}    publicstring modifiedBy  {get;set;}    publicString createdBy  {get;set;}    publicString HistoryType  {get;set;}    publicString Status   {get;set;}    publicinteger num   {get;set;} } Thanks September4,2012·Like0·Follow0 BestAnswerchosenbyAdmin (SalesforceDevelopers) RepsGprobelmsolved.Ididntusevishalsuggestionalthoughhedidgivemealightbulbmoment. Thisismycode: Ifirstputmydatainvariable‘mhistroy’andthensortthedataintovariable‘afterSort’.IthencallafterSortinmypageblocktable      privatevoidsortmhistory(){                      integertargetsize=mhistroy.size();                      while(afterSort.size()!=targetsize){                 findMinDateValue();           }                }          privatevoidfindMinDateValue(){            //datetimedt=Datetime.newInstance(2999,12,31,00,00,00);//ifascending           datetimedt=Datetime.newInstance(1900,12,31,00,00,00);//ifdecending                      integeri=0;           integermini=0;                      for(i=0;i!=mhistroy.size();i++){                 //if(mhistroy[i].createdDated>dt){//ifascending                 if(mhistroy[i].createdDated>dt){//ifdecending                       dt=mhistroy[i].createdDated;                       mini=i;                 }           }                      afterSort.add(mhistroy[mini]);           mhistroy.remove(mini);               }September6,2012·Like0·Dislike0 vishal@forceIfyoucanfollowthisapproach,Ithinkitwouldwork. Createanotherlistofthesamewrapperlist(you'llhaveyoursorteddatainthisone). Now,iteratethroughcurrentlist,collectallthedatesinalist. uselist.sort();tosortyourdates. Since,thissortsinascendingdirectionautomatically,iteratethroughthisfromthelastrecord for(i=list.size()-1;i==0;i--)//somethinglikethis Compareyourdates: for(historyh:yourlist){    for(i=list.size()-1;i==0;i--)    {         if(list[i]==h.createdDated)         {              yournewlist.add(h);         }    }} thelist"yournewlist"willhaverecordssortedindescendingorder.HopethishelpsSeptember4,2012·Like0·Dislike0 RepsGThanksVishal,Iwillgivethisago.  September4,2012·Like0·Dislike0 RepsGprobelmsolved.Ididntusevishalsuggestionalthoughhedidgivemealightbulbmoment. Thisismycode: Ifirstputmydatainvariable‘mhistroy’andthensortthedataintovariable‘afterSort’.IthencallafterSortinmypageblocktable      privatevoidsortmhistory(){                      integertargetsize=mhistroy.size();                      while(afterSort.size()!=targetsize){                 findMinDateValue();           }                }          privatevoidfindMinDateValue(){            //datetimedt=Datetime.newInstance(2999,12,31,00,00,00);//ifascending           datetimedt=Datetime.newInstance(1900,12,31,00,00,00);//ifdecending                      integeri=0;           integermini=0;                      for(i=0;i!=mhistroy.size();i++){                 //if(mhistroy[i].createdDated>dt){//ifascending                 if(mhistroy[i].createdDated>dt){//ifdecending                       dt=mhistroy[i].createdDated;                       mini=i;                 }           }                      afterSort.add(mhistroy[mini]);           mhistroy.remove(mini);               }ThiswasselectedasthebestanswerKRITHIKABALASUBRAMANIANShoulditbe mhistroy[i].createdDated>dtor mhistroy[i].createdDated



請為這篇文章評分?