You need to sign in to do that
Don't have an account?
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
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
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
Thanks Vishal, I will give this a go.
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);
}