function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RepsGRepsG 

Wrapper Class - Sort List by Date

Hi Guys,

 

I was wondering if anybody can help me sort a wrapper class by Date.

 

I have data from three object going into a list variable. How do i then sort the list by date (decending)?

 

public class history
{
     public String  title   {get; set;}
     public String  Description   {get; set;}
     public String  primaryAccount  {get; set;}
     public datetime createdDated   {get; set;}
     public string  modifiedBy  {get; set;}
     public String  createdBy  {get; set;}
     public String  HistoryType  {get; set;}
     public String  Status   {get; set;}
     public integer  num   {get; set;} 
}

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
RepsGRepsG

probelm solved. I didnt use vishal suggestion although he did give me a light bulb moment.

 

This is my code:

 

I first put my data in variable ‘mhistroy’ and then sort the data into variable ‘afterSort’. I then call afterSort in my pageblocktable

 

      private void sortmhistory(){

           

            integer targetsize = mhistroy.size();

           

            while(afterSort.size() != targetsize){

                  findMinDateValue();

            }

           

      }

     

      private void findMinDateValue(){

 

            //datetime dt = Datetime.newInstance(2999, 12, 31, 00, 00, 00); // if ascending

            datetime dt = Datetime.newInstance(1900, 12, 31, 00, 00, 00); // if decending

           

            integer i = 0;

            integer mini = 0;

           

            for (i = 0; i != mhistroy.size() ; i ++){

                  //if(mhistroy[i].createdDated > dt){ // if ascending

                  if(mhistroy[i].createdDated > dt){ // if decending

                        dt = mhistroy[i].createdDated;

                        mini = i;

                  }

            }

           

            afterSort.add(mhistroy[mini]);

            mhistroy.remove(mini);

           

      }

All Answers

vishal@forcevishal@force

If you can follow this approach, I think it would work.

 

Create another list of the same wrapper list (you'll have your sorted data in this one).

 

Now, iterate through current list, collect all the dates in a list<date>.

 

use list.sort(); to sort your dates.

 

Since, this sorts in ascending direction automatically, iterate through this from the last record

 

for (i = list.size() - 1; i == 0 ; i --) // something like this

 

Compare your dates :

 

for(history h : yourlist)

{

     for(i = list.size() - 1; i == 0 ; i --)

     {

          if(list[i] == h.createdDated)

          {

               yournewlist.add(h);

          }

     }
}

 

the list " yournewlist" will have records sorted in descending order. Hope this helps

RepsGRepsG

Thanks Vishal, I will give this a go.

 

 

RepsGRepsG

probelm solved. I didnt use vishal suggestion although he did give me a light bulb moment.

 

This is my code:

 

I first put my data in variable ‘mhistroy’ and then sort the data into variable ‘afterSort’. I then call afterSort in my pageblocktable

 

      private void sortmhistory(){

           

            integer targetsize = mhistroy.size();

           

            while(afterSort.size() != targetsize){

                  findMinDateValue();

            }

           

      }

     

      private void findMinDateValue(){

 

            //datetime dt = Datetime.newInstance(2999, 12, 31, 00, 00, 00); // if ascending

            datetime dt = Datetime.newInstance(1900, 12, 31, 00, 00, 00); // if decending

           

            integer i = 0;

            integer mini = 0;

           

            for (i = 0; i != mhistroy.size() ; i ++){

                  //if(mhistroy[i].createdDated > dt){ // if ascending

                  if(mhistroy[i].createdDated > dt){ // if decending

                        dt = mhistroy[i].createdDated;

                        mini = i;

                  }

            }

           

            afterSort.add(mhistroy[mini]);

            mhistroy.remove(mini);

           

      }

This was selected as the best answer
KRITHIKA BALASUBRAMANIANKRITHIKA BALASUBRAMANIAN
Should it be mhistroy[i].createdDated > dt or mhistroy[i].createdDated < dt for descending. My ascending sort s woking but descending is not.