I need to use this wrapper class to sort the returned articles by the CaseAssociationCount in the KnowledgeArticle object. Just having a bit of trouble after getting the wrapper put together.

Here is my lightning component:


<aura:component implements="forceCommunity:availableForAllPageTypes" controller="GetHBArticles">
    <aura:attribute name="searchVar" type="String"  />
    <aura:attribute name="articles" type="SObject[]"/>

    <ui:inputText class="form-control" value="{!v.searchVar}"

    <ui:button label="Search" press="{!c.getArts}"/>
    <aura:iteration var="articles" items="{!v.articles}">
        <p>Article ID: {!articles.kav.Id} | Article Title: {!articles.kav.Title} | Article Type: {!articles.kav.ArticleType}</p>


Client-side Controller

    getArts: function(cmp){

        var action = cmp.get("c.getArticlesList");
        action.setParams({ searchTerm : cmp.get("v.searchVar") });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.articles", response.getReturnValue());

Wrapper class and server controller

public with sharing class knowledgeArticleWrapper {

    public KnowledgeArticleVersion kav {get;set;}
    public list<KnowledgeArticle> lstcasearticle {get;set;}
    public integer caseAssociationCount {get;set;}

    public knowledgeArticleWrapper(){
        lstcasearticle  = new list<KnowledgeArticle>();

public with sharing class GetHBArticles {

    public static List<knowledgeArticleWrapper> getArticlesList(String searchTerm){
        List<knowledgeArticleWrapper> articlesWrapper = new List<knowledgeArticleWrapper> ();
        List<KnowledgeArticleVersion> articles = [FIND :searchTerm RETURNING KnowledgeArticleVersion
                                                  (Id, Title, ArticleType 
                                                   WHERE PublishStatus='online' AND Language = 'en_US' 
                                                   AND ArticleType IN ('Troubleshooting__kav', 'How_To__kav', 'FAQ__kav'))
                                                  WITH DATA CATEGORY Topics__c AT *DATACATEGORYNAME*][0];

        for(KnowledgeArticleVersion kav :articles ){
            knowledgeArticleWrapper kwrap = new knowledgeArticleWrapper();
            kwrap.kav = kav;
        return articlesWrapper;

1 Answer 1


To get the list sorted you would add the sort call to the end of getArticlesList:

return articlesWrapper;

and add this to the wrapper class:

public with sharing class knowledgeArticleWrapper implements Comparable {
    public Integer compareTo(Object o) {
        knowledgeArticleWrapper that = (knowledgeArticleWrapper) o;
        if (this.caseAssociationCount < that.caseAssociationCount) return -1;
        else if (this.caseAssociationCount > that.caseAssociationCount) return 1
        else return 0;
  • return this.caseAssociationCount - that.caseAssociationCount;
    – sfdcfox
    Apr 13, 2016 at 18:13
  • ^^ Could you explain this a bit? @sfdfox ?
    – Allen Mann
    Apr 13, 2016 at 18:18
  • 1
    @AllenMann The Comparable interface just requires a positive, zero or negative value as the return value and for the case where it is two numbers being compared just subtracting them gives the right outcome. Personally, I prefer the more long winded code that explicitly covers the 3 cases as it is clearer for most people when they read the code.
    – Keith C
    Apr 13, 2016 at 18:49

You must log in to answer this question.

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