+ Start a Discussion

how to sort list with unrelated objects by created date

I created a VF page to mock the notes and attachments related list. Now i have it to where the attachments are inserted on top of the notes. How can I get them  sorted by created date to make them mix in with each other instead of attachments being on top and notes being underneath. Any help is appreciated. Thanks.

global class NotesAttachment{
    public List<Note> n1 {get;set;}
    public List<Tuple> combinations{get;set;}
    public string ecid{get;set;}
    public string dcid{get;set;}
    public Datetime CreatedDate;
    String strid = System.currentPagereference().getParameters().get('id');
    public sObject Tuppy;   
    public List<Tuple> pageCombinations {get;set;}
    private Integer pageNumber;
    private Integer PageSize;
    private Integer totalPageNumber;
    public Integer getPageNumber(){
       return pageNumber;
    public Integer getPageSize(){
       return pageSize;
    public Boolean getPreviousButtonEnabled(){
           return !(pageNumber> 1);
    public Integer getTotalPageNumber(){
       if(totalPageNumber == 0 && combinations != null){

        totalPageNumber = combinations.size() / pageSize;
        Integer mod = combinations.size() - (totalPageNumber * pageSize);
        if(mod > 0)
    return totalPageNumber;
    public NotesAttachment(ApexPages.StandardController stdController)
       //rlucas Paging
       pageNumber = 0;
       totalPageNumber = 0;
       pageSize = 7;

    //rlucas view note/attachment
    public PageReference ViewData()
        combinations = null;
        totalPageNumber = 0;
        return null;
    private void BindData(Integer newPageIndex){
        //if(combinations == null)
        combinations = new List<Tuple>();
        Tuple tr;
        List<Attachment> atts =[SELECT Id, Name, createdbyid, createdDate, lastmodifieddate, ContentType, BodyLength, parentID FROM Attachment where parentid=:ApexPages.currentPage().getParameters().get('Id') order by createddate desc];
        List<Note> nt = [select Body, CreatedById, createdDate,Id, Title, parentID, LastModifieddate from Note where parentid=:ApexPages.currentPage().getParameters().get('Id') order by createdDate desc];
        for(Attachment att : atts){
                //Instantiating the wrapper sObject
                tr = new Tuple();
                //Assigning the wrapper variables
                tr.title = att.Name;
                tr.parentID = att.parentID;
                tr.CreatedById = att.CreatedById;
                tr.body = att.ContentType;
                tr.id = att.id;
                tr.LastModifieddate = att.LastModifieddate;
                //*Add everything to the List then and there*/
        for(Note con : nt){
            tr = new Tuple();
            tr.title = con.title;
            tr.parentID = con.parentID;
            tr.CreatedById = con.CreatedById;
            tr.body = con.Body;
            tr.id = con.id;
            tr.LastModifieddate = con.LastModifieddate;
            /*Add the TableRow to the List then and there*/
       pageCombinations = new List<Tuple>();
       Transient Integer counter = 0;
       Transient Integer min = 0;
       Transient Integer max = 0;
       if(newPageIndex > pageNumber){
            min = pageNumber * pageSize;
            max = newPageIndex * pageSize;
        } else {
           max = newPageIndex * pageSize;
           min = max - pageSize;
        for(Tuple t : combinations){
           counter ++;
           if(counter > min && counter <=max)
        pageNumber = newPageIndex;
        if(pageCombinations == null || pageCombinations.size() <= 0)
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Data not available for this view.'));
    catch(Exception ex)
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,ex.getMessage()));
    public Boolean getNextButtonDisabled(){
       if(combinations == null) return true;
       return((pageNumber * pageSize) >= combinations.size());

    public PageReference nextBtnClick(){
        BindData(pageNumber + 1);
    return null;
    public PageReference previousBtnClick(){
        BindData(pageNumber - 1);
    return null;

    //wrapper class
    global class Tuple
      public String id {get; set;}
      public String body  {get; set;}
      public String Title {get; set;}
      public String parentID {get; set;}
      public DateTime LastModifieddate{get; set;}
      public String createdbyid  {get; set;}
      public DateTime CreatedDate {get; set;}
      public String Name {get; set;}
      public String ContentType {get; set;}

    public NotesAttachment(){
        combinations = new List<Tuple>();
Your query looks good,it should sort the results returned based on CreatedDate.

What is the issue you are facing ??
It is sorting each query by created dated but for example lets say i create a note, an attachment and another note it will be displayed as attachment, note and note. This is because the list is not being sorted by created just the queries. I am asking how can i get the list sorted by createddate or have a nested loop to where i do not have to sort. I thought it would be something like this: http://nirmalchristopher.blogspot.com/2013/10/been-out-for-quite-some-time-its-been.html

But I was wrong. Is there anything you can suggest? I tried implementing comparable but that was too much of a hassle. Thanks.
If the resulted list returned by query is in sorted format,I don't see any reason why this would not display on VF page in sorted format.

Can you run your query in SOQL explorer or Workbench to see if the list is coming in sorted order.
Here is a screen shot of what it is doing....even though i created the note after the attachment the attachment is placed on top. How can I fix this?It is in sorted format as far as each note will be sort by createddate and each attachment will be sorted by createddate but i need each note and attachment to be sorted by createddate: