0

I have a List<ListWrapper> comprised of List<Knowledge__kav> and List<Community_Event__c>. I would like to sort List<ListWrapper> by date but the two inner lists have different date fields. How can I sort?

Code Snippets`public class WhatsNewController {

public class ListWrapper{
    @auraenabled
    Knowledge__kav article {get; set;}
    @auraenabled
    Community_Event__c event {get; set;}
}

public static List<ListWrapper> getWhatsNew(){  
    //Get all events
    String eventQuery = 'Select Id, Title FROM Knowledge__kav WHERE Title != null ';
    eventQuery = eventQuery + 'ORDER BY CreatedDate LIMIT 20';
    List<Community_Event__c> events = database.query(eventQuery);

    //Get all articles with filters (kav) applied
    String articleQuery = 'Select Id, Name FROM Community_Event__c ';

    articleQuery = articleQuery + 'ORDER BY LastPublishedDate LIMIT 20';
    List<Knowledge__kav> articles = database.query(articleQuery);

    List<ListWrapper> listEw = new List<ListWrapper>();
    for(Community_Event__c cE : events){
        ListWrapper eW = new ListWrapper();
        eW.event = cE;
        listEw.add(eW);
        listEw.();

                  for(Integer j=0;j<listEw.size();j++)
                    System.debug('list values='+ listEw[j]);
    }

    List<ListWrapper> listAw = new List<ListWrapper>();
    for(Knowledge__kav kA : articles){
        ListWrapper aW = new ListWrapper();
        aW.article = kA;
        listAw.add(aW);

                  for(Integer j=0;j<listAw.size();j++)
                    System.debug('list values='+ listAw[j]);
    }

    //Sort the lists ----> listEw listAw sort by date and put into following list

    List<ListWrapper> listOfWrappers = new List<ListWrapper>();
        listOfWrappers.addAll(listAw);
        listOfWrappers.addAll(listEw);
        listOfWrappers.sort();
        //listOfWrappers.Sort((x, y) => DateTime.Compare(x.Created, y.Created));


    return listOfWrappers;

}`
2
  • Can you please add some code? Sep 11, 2019 at 18:39
  • Code Added. Some of it like the Debug may be incorrect as well but the queries and list adding works correctly which is the key part
    – Trace
    Sep 11, 2019 at 19:03

1 Answer 1

5

You can do this using the standard sort() method provided by the List type if your ListWrapper class implements the Comparable interface

You'll have to decide for yourself how to compare two different instances of your ListWrapper, but the general pattern is going to be the same

global Integer compareTo(Object o){
    // typecast the argument so that you can access the data it contains
    ListWrapper otherWrapper = (ListWrapper)o;

    if(criteria 1){
        return -1; // Object o's value is greater than "this" object's value
    else if (criteria 2){
        return 0; // elements are equal
    }
    else{
        return 1; // Object o's value is less than "this" object's value
    }
}

You could:

  • pick which date to compare first, and only do a comparison against the other date if the first dates are equal
  • find the earliest (or latest) date out of the two, and compare that date against the same date (earliest or latest) for the other record
  • some other combination of comparisons

The remaining gotcha here is that List.sort() always sorts in ascending order. If you need to sort in descending order, you can just flip the return values for the non-equal cases.

Ex.

// Assuming an Apex class with an Integer variable
global Integer compareTo(Object o){
    Integer otherInt = (Integer)o;

    if(this.myInt < otherInt.myInt){
        // To have list.sort() sort ascending, we'd normally return -1 here
        //   (which indicates that Object o should come before this instance in the list)
        // By returning +1, we end up saying that this instance should come before Object o
        //   in the list
        // This ends up having the same effect as if we sorted the list in descending order
        return 1;
    }else if(this.myInt == otherInt.myInt){
        return 0;
    }else{
        return -1;
    }
}
2
  • Can you show me what It would look like with Lists and DateTimes instead of Integers? Or will I have to use Integers for mine as well? Sorry new to coding
    – Trace
    Sep 11, 2019 at 19:14
  • 1
    @user73937 You have to return integer values (it's the return type the interface requires) but, beyond that, you can use any data type that supports comparison operators. this.FirstDate < otherWrapper.FirstDate is perfectly valid. I'd encourage you to read through the documentation I linked to on the comparable interface and try to apply the example Salesforce gives to your situation yourself. If you get stuck in implementing it, then you can ask another question to help get past that particular issue. I find failure (and eventual success) to be a better teacher than a copy-paste solution.
    – Derek F
    Sep 11, 2019 at 19:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .